字符串切割函数汇总。
http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html
1题目描述
请实现一个算法,确定一个字符串的所有字符是否全都不同。这里我们要求不允许使用额外的存储结构。
给定一个string iniString,请返回一个bool值,True代表所有字符全都不同,False代表存在相同的字符。保证字符串中的字符为ASCII字符。字符串的长度小于等于3000。
测试样例:
"aeiou"
返回:True
"BarackObama"
返回:False
1.利用map做
class Different { public: bool checkDifferent(string iniString) { // write code here map<char, int>a; int i; for (i = 0; i<iniString.size(); i++){ a[iniString[i]]++; } for (auto it = a.begin(); it!=a.end(); it++){ if (it->second>1) return false; } return true; } };
View Code
2题目描述
请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)。
给定一个string iniString,请返回一个string,为翻转后的字符串。保证字符串的长度小于等于5000。
测试样例:
"This is nowcoder"
返回:"redocwon si sihT"
对调字符串。
class Reverse { public: string reverseString(string iniString) { // write code here int len = iniString.size(); int ch; for(int i=0;i<len/2;i++){ ch = iniString[i]; iniString[i] = iniString[len-i-1]; iniString[len-i-1] = ch; } return iniString; } };
View Code
3题目描述
给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串重点空格。
给定一个string stringA和一个string stringB,请返回一个bool,代表两串是否重新排列后可相同。保证两串的长度都小于等于5000。
测试样例:
"This is nowcoder","is This nowcoder"
返回:true
"Here you are","Are you here"
返回:false
class Same { public: bool checkSam(string stringA, string stringB) { sort(stringA.begin(),stringA.end()); sort(stringB.begin(),stringB.end()); return stringA.compare(stringB)==0; } };
View Code
4题目描述
请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。
给定一个string iniString 为原始的串,以及串的长度 intlen, 返回替换后的string。
测试样例:
"Mr John Smith”,13
返回:"Mr%20John%20Smith"
”Hello World”,12
返回:”Hello%20%20World”
class Replacement { public: string replaceSpace(string iniString, int length) { // write code here string s = "%20"; int i=0; while(iniString[i]!='