好的!下面是对每道题目的C++代码实现,帮助你练习和理解字符串的操作。
1. 字符串长度
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream> #include <string> using namespace std;
int main() { string str; cout << "请输入字符串: "; getline(cin, str); cout << "字符串长度为: " << str.length() << endl; return 0; }
|
2. 字符串反转
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <iostream> #include <string> #include <algorithm> using namespace std;
int main() { string str; cout << "请输入字符串: "; getline(cin, str); reverse(str.begin(), str.end()); cout << "反转后的字符串: " << str << endl; return 0; }
|
3. 字符串比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <iostream> #include <string> using namespace std;
bool compareStrings(string str1, string str2) { if (str1.length() != str2.length()) return false; for (int i = 0; i < str1.length(); i++) { if (str1[i] != str2[i]) return false; } return true; }
int main() { string str1, str2; cout << "请输入第一个字符串: "; getline(cin, str1); cout << "请输入第二个字符串: "; getline(cin, str2); if (compareStrings(str1, str2)) cout << "两个字符串相等" << endl; else cout << "两个字符串不相等" << endl; return 0; }
|
4. 统计字符串中的字符个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <iostream> #include <map> #include <string> using namespace std;
int main() { string str; cout << "请输入字符串: "; getline(cin, str); map<char, int> charCount;
for (char ch : str) { charCount[ch]++; }
for (auto &pair : charCount) { cout << pair.first << ": " << pair.second << endl; } return 0; }
|
5. 大写字母转小写
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <iostream> #include <string> #include <algorithm> using namespace std;
int main() { string str; cout << "请输入字符串: "; getline(cin, str); transform(str.begin(), str.end(), str.begin(), ::tolower); cout << "转换后的字符串: " << str << endl; return 0; }
|
6. 查找子串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <iostream> #include <string> using namespace std;
int main() { string str, substr; cout << "请输入主字符串: "; getline(cin, str); cout << "请输入子字符串: "; getline(cin, substr);
int pos = str.find(substr); if (pos != string::npos) { cout << "子串起始位置: " << pos << endl; } else { cout << "子串未找到" << endl; } return 0; }
|
7. 字符串旋转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <iostream> #include <string> using namespace std;
string rotateString(string str, int steps) { int n = str.length(); steps = steps % n; return str.substr(n - steps) + str.substr(0, n - steps); }
int main() { string str; int steps; cout << "请输入字符串: "; getline(cin, str); cout << "请输入右移步数: "; cin >> steps; cout << "旋转后的字符串: " << rotateString(str, steps) << endl; return 0; }
|
8. 统计单词数量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <iostream> #include <string> #include <sstream> using namespace std;
int main() { string str; cout << "请输入一句话: "; getline(cin, str);
stringstream ss(str); string word; int count = 0;
while (ss >> word) { count++; }
cout << "单词数量: " << count << endl; return 0; }
|
9. 删除特定字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <iostream> #include <string> using namespace std;
int main() { string str; char ch; cout << "请输入字符串: "; getline(cin, str); cout << "请输入要删除的字符: "; cin >> ch;
str.erase(remove(str.begin(), str.end(), ch), str.end());
cout << "删除后的字符串: " << str << endl; return 0; }
|
10. 回文判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <iostream> #include <string> using namespace std;
bool isPalindrome(string str) { int i = 0, j = str.length() - 1; while (i < j) { if (str[i] != str[j]) return false; i++; j--; } return true; }
int main() { string str; cout << "请输入字符串: "; getline(cin, str);
if (isPalindrome(str)) { cout << "是回文字符串" << endl; } else { cout << "不是回文字符串" << endl; } return 0; }
|
11. 最长公共前缀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <iostream> #include <vector> #include <string> using namespace std;
string longestCommonPrefix(vector<string>& strs) { if (strs.empty()) return "";
string prefix = strs[0]; for (int i = 1; i < strs.size(); i++) { while (strs[i].find(prefix) != 0) { prefix = prefix.substr(0, prefix.length() - 1); if (prefix.empty()) return ""; } } return prefix; }
int main() { vector<string> strs = {"flower", "flow", "flight"}; cout << "最长公共前缀为: " << longestCommonPrefix(strs) << endl; return 0; }
|
12. 字符串压缩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <iostream> #include <string> using namespace std;
string compressString(string str) { string result = ""; int count = 1;
for (int i = 1; i <= str.length(); i++) { if (i < str.length() && str[i] == str[i-1]) { count++; } else { result += str[i-1]; result += to_string(count); count = 1; } } return result.length() < str.length() ? result : str; }
int main() { string str; cout << "请输入字符串: "; getline(cin, str);
cout << "压缩后的字符串: " << compressString(str) << endl; return 0; }
|
13. 最长不重复子串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <iostream> #include <string> #include <unordered_map> using namespace std;
int lengthOfLongestSubstring(string str) { unordered_map<char, int> charIndex; int maxLength = 0, start = 0;
for (int i = 0; i < str.length(); i++) { if (charIndex.find(str[i]) != charIndex.end() && charIndex[str[i]] >= start) { start = charIndex[str[i]] + 1; } charIndex[str[i]] = i; maxLength = max(maxLength, i - start + 1); }
return maxLength; }
int main() { string str; cout << "请输入字符串: "; getline(cin, str);
cout << "最长不重复子串的长度: " << lengthOfLongestSubstring(str) << endl; return 0; }
|
14. 字符串排列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> #include <string> #include <algorithm> using namespace std;
void permute(string str, int l, int r) { if (l == r) cout << str << endl; else { for (int i = l; i <= r; i++) { swap(str[l], str[i]); permute(str, l + 1, r); swap(str[l], str[i]); } } }
int main() { string str; cout << "请输入字符串: "; getline(cin, str);
permute(str, 0, str.length() - 1); return 0; }
|
15. 字符串中的第一个唯一字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <iostream> #include <unordered_map> #include <string> using namespace std;
int firstUniqueChar(string str) { unordered_map<char, int> charCount;
for (char ch : str) { charCount[ch]++; }
for (int i = 0; i < str.length(); i++) { if (charCount[str[i]] == 1) { return i; } } return -1; }
int main() { string str; cout << "请输入字符串: "; getline(cin, str);
int index = firstUniqueChar(str); if (index != -1) cout << "第一个唯一字符的索引是: " << index
<< endl; else cout << "没有唯一字符" << endl; return 0; }
|