字符串是程序员求职笔试中必考题型,很能考查出编程的基础。下文选取了几个常见的考题和大家进行分享。
1、编写函数,实现把一个char组成的字符串循环右移n位。如abcdehi,n=2。则输出hiabcde。
#include "iostream"
using namespace std;
const int MAX_LEN = 20;
void LoopMove(char* cpStr, int iSteps)
{
//注意,在整个处理过程中,cpStr的最后字符都没有涉及处理
char cTempArray[MAX_LEN];
size_t szStrLength = strlen(cpStr);
size_t in = szStrLength -iSteps;
memcpy(cTempArray, cpStr + in, iSteps);
memcpy(cTempArray + iSteps, cpStr, in);
memcpy(cpStr, cTempArray, szStrLength);
cTempArray[szStrLength + 1] = '\0';
cout << cTempArray << endl;
}
int main()
{
char ctemp[] = "abcdefghi";
LoopMove(ctemp, 2);
cout << ctemp << endl;
return 1;
}
2、输入一行字符串,找出其中出现的相同且长度最长的字符串,输出它及其首字符的位置。如yyabcdabjcabceg,则输出为abc,3。
大体思路:把字符串yyabcdabjcabceg拆解: yyabcdabjcabceg yabcdabjcabceg abcdabjcabceg ... ceg eg g 然后对字符串进行排序,比较相邻字符串的前驱,求最长的共公前驱。 在我们的程序中的体现,我们没有用这种方法,因为这种方法在排序中会用很多时间。我们借用了C++的实现函数find来巧妙的实现。 注:basic_string::substr basic_string substr(size_type pos = 0, size_type n = npos) const; The member function returns an object whose controlled sequence is a copy of up to n elements of the controlled sequence beginning at position pos. 返回一个从指定位置开始,并具有指定长度的子字符串。 参数 pos 必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 0。 n 可选项。返回的子字符串中包含的字符数。 备注 如果 n 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。 在VS中测试,如是n是负数或大于主串的总长度,则输出是pos开始到主串末尾的字符。 示例代码
#include "iostream" #include "string" using namespace std; int main() { string strInput; cout << "Input a string: " << endl; cin >> strInput; string strTemp; for (size_t i = strInput.length() - 1; i > 1; i--) { for (size_t j = 0; j < strInput.length(); j++) { if ((i + j) <= strInput.length()) { size_t szForw = 0; size_t szBacw = 0; strTemp = strInput.substr(j, i); szForw = strInput.find(strTemp); szBacw = strInput.rfind(strTemp); if (szBacw != szForw) { cout << strTemp << " " << szForw + 1 << endl; return 0; } } } } return 1; }
3、实现strstr()功能。如主串是12345678,子串是234,则返回2345678。
#include "iostream" #include "string" using namespace std; const char* strstr1(const char* strMainString, const char* strSubString) { for (size_t i = 0; strMainString[i]; i++) { size_t iTempj = 0; size_t iTempi = i; if (strMainString[iTempi] == strSubString[iTempj]) { while(strMainString[iTempi++] == strSubString[iTempj++]) { if (strSubString[iTempj] == '\0') return &strMainString[i]; } } } return NULL; } int main() { char str1[] = "12345678"; char str2[] = "234"; const char *str3 = strstr1(str1, str2); cout << str3 << endl; return 1; }
4、将一句话中的单词倒置,标点符号不倒换。如“i come from tianjin.”,倒换后变成“tianjin. from come i”。
大体思路:先把整个字符串调整,再针对每个单词进行调整。 示例代码 view sourceprint? #include "iostream" #include "string" //#include "functional" //#include "algorithm" using namespace std; int main() { cout << "Input a string: " << endl; string strOfaLine; getline(cin, strOfaLine); size_t szStrLength = strOfaLine.length(); size_t szTempbeg = 0; size_t szTempend = szStrLength - 1; //第一步,全局交换 while(szTempbeg < szTempend) { swap<char>(strOfaLine[szTempbeg++], strOfaLine[szTempend--]); } //第二步,局部交换 size_t szTempi = 0; while (strOfaLine[szTempi]) { if (strOfaLine[szTempi] != ' ') { szTempbeg = szTempi; while(strOfaLine[szTempi] != '\0' && strOfaLine[szTempi] != ' ') { szTempi++; } szTempi--; szTempend = szTempi; } while(szTempbeg < szTempend) { swap<char>(strOfaLine[szTempbeg++], strOfaLine[szTempend--]); } szTempi++; } cout << strOfaLine << endl; return 1; }
5、找出字符串中最长最短单词并统计词数
/*编写程序计算sentence中有多少个单词,并输出其中最长和最短的单词。 如果有多个最长或最短的单词,则将其全部输出。 其中 string line1="We were her pride of 10 she named us:"; string line2="Benjamin,Phoenix,the Prodigal"; string line3="and perspicacious pacific suzanne"; string sentence = line1+' '+line2+' '+line3; */ #include "stdafx.h" #include <vector> #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { string line1="We were her pride of 10 she named us:"; string line2="Benjamin,Phoenix,the Prodigal"; string line3="and perspicacious pacific suzanne"; string sentence = line1+' '+line2+' '+line3; string separators(" \t;,\v\t\n\r\f"); string word; vector<string>longestWord,shortestWord; string::size_type maxLen,minLen,wordLen,count=0; string::size_type start_pos=0,end_pos=0; while ((start_pos=sentence.find_first_not_of(separators,start_pos))!=string::npos) { ++count; end_pos = sentence.find_first_of(separators,start_pos); if(end_pos==string::npos) { wordLen = sentence.size()-start_pos; } else { wordLen = end_pos-start_pos; } word.assign(sentence.begin()+start_pos,sentence.begin()+start_pos+wordLen); start_pos=sentence.find_first_not_of(separators,end_pos); if(count==1) { minLen=maxLen=wordLen; longestWord.push_back(word); shortestWord.push_back(word); } else { if(wordLen>maxLen) { maxLen = wordLen; longestWord.clear(); longestWord.push_back(word); } else if (wordLen<minLen) { minLen = wordLen; shortestWord.clear(); shortestWord.push_back(word); } else if(wordLen==minLen) { shortestWord.push_back(word); } else if(wordLen==maxLen) longestWord.push_back(word); } } cout<<"word count="<<count<<" words"<<endl; cout<<"longest words are:"; for(vector<string>::iterator i=longestWord.begin();i!=longestWord.end();i++) { cout<<*i<<endl; } cout<<"shortest words are:"; for(vector<string>::iterator j=shortestWord.begin();j!=shortestWord.end();j++) { cout<<*j<<endl; } return 0; }