左填充-LintCode

来源:互联网 发布:购买高权重网站域名 编辑:程序博客网 时间:2024/04/28 03:06

实现一个leftpad库,如果不知道什么是leftpad可以看样例

样例:

leftpad("foo", 5)>> "  foo"leftpad("foobar", 6)>> "foobar"leftpad("1", 2, "0")>> "01"
#ifndef C524_H#define C524_H#include<iostream>#include<string>using namespace std;class StringUtils {public:    /*    * @param originalStr: the string we want to append to    * @param size: the target length of the string    * @param padChar: the character to pad to the left side of the string    * @return: A string    */    static string leftPad(string &originalStr, int size, char padChar = ' ') {        // write your code here        int num = size - originalStr.size();        while (num > 0)        {            originalStr = padChar + originalStr;            num--;        }        return originalStr;    }}; #endif