C++中的字符串
一字符与整数的联系——ASCII码
1.每个常用字符都对应一个-128~127的数字,二者之间可以相互转化
2.常用ASCII值:’A’-‘Z’ 是65~90,’a’-‘z’是97-122,’0’-‘9’是48-57。
3.字符可以参与运算,运算时会将其当做整数
二字符数组
1.字符串就是字符数组加上结束符’\0’。
2.字符数组的输入输出
输入:
- fgets (s,100000.stdin)
-
cin.getline(s,100)
- string类型 getline(cin,s)
输出:
1.Puts(s)
2.printf(“%s”, s.c_str());
3.字符数组的常用操作
引入头文件:#include <string.h>
(1) strlen(str),求字符串的长度,不包含’\0’
(2) strcmp(a, b),比较两个字符串的大小,a < b 返回-1,a == b 返回0,a > b返回1。
(3) strcpy(a, b),将字符串b复制给从a开始的字符数组
三标准库类型 string
注意
可变长的字符序列,比字符数组更加好用。需要引入头文件:
#include < string >
输入输出
基本操作
1.string的empty和size操作
2.string 的比较
3.string对象赋值/相加
string对象中的字符
1.将string对象当成字符数组来处理
2.使用基于范围的for语句
四例题求解
课内例题
1.字符串长度
注意:fgets函数会把回车也读进来
#include <cstdio>
int main()
{
char str[101];
fgets(str, 101, stdin);
int len = 0;
for (int i = 0; str[i] && str[i] != '\n'; i ++ ) len ++ ;
printf("%d\n", len);
return 0;
}
2.字符串中的数字个数
#include <cstdio>
int main()
{
char str[101];
fgets(str, 101, stdin);
int cnt = 0;
for (int i = 0; str[i]; i ++ )
if (str[i] >= '0' && str[i] <= '9')
cnt ++ ;
printf("%d\n", cnt);
return 0;
}
3.循环相克令
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while (n -- )
{
string a, b;
cin >> a >> b;
int x, y;
if (a == "Hunter") x = 0;
else if (a == "Bear") x = 1;
else x = 2;
if (b == "Hunter") y = 0;
else if (b == "Bear") y = 1;
else y = 2;
if (x == y) puts("Tie");
else if (x == (y + 1) % 3) puts("Player1");
else puts("Player2");
}
return 0;
}
4.字符串加空格
#include <iostream>
using namespace std;
int main()
{
string a;
getline(cin, a);
string b;
for (auto c : a) b = b + c + ' ';
b.pop_back(); // 把最后一个字符删掉
cout << b << endl;
return 0;
}
**5.替换字符 **
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
char str[31];
scanf("%s", str);
char c;
scanf("\n%c", &c);
for (int i = 0; str[i]; i ++ )
if (str[i] == c)
str[i] = '#';
puts(str);
return 0;
}
6.字符串插入
#include <iostream>
using namespace std;
int main()
{
string a, b;
while (cin >> a >> b)
{
int p = 0;
for (int i = 1; i < a.size(); i ++ )
if (a[i] > a[p])
p = i;
cout << a.substr(0, p + 1) + b + a.substr(p + 1) << endl;
}
return 0;
}
7.只出现一次的字符
#include <iostream>
#include <cstring>
using namespace std;
int cnt[26];
char str[100010];
int main()
{
cin >> str;
for (int i = 0; str[i]; i ++ ) cnt[str[i] - 'a'] ++ ;
for (int i = 0; str[i]; i ++ )
if (cnt[str[i] - 'a'] == 1)
{
cout << str[i] << endl;
return 0;
}
puts("no");
return 0;
}
课间习题
1.字符串匹配
#include <iostream>
using namespace std;
int main()
{
double k;
string a, b;
cin >> k >> a >> b;
int cnt = 0;
for (int i = 0; i < a.size(); i ++ )
if (a[i] == b[i])
cnt ++ ;
if ((double)cnt / a.size() >= k) puts("yes");
else puts("no");
return 0;
}
2.忽略大小写比较字符串大小
#include <cstdio>
#include <cstring>
int main()
{
char a[100], b[100];
fgets(a, 100, stdin);
fgets(b, 100, stdin);
if (a[strlen(a) - 1] == '\n') a[strlen(a) - 1] = 0; // 去掉末尾回车
if (b[strlen(b) - 1] == '\n') b[strlen(b) - 1] = 0; // 去掉末尾回车
for (int i = 0; a[i]; i ++ )
if (a[i] >= 'A' && a[i] <= 'Z')
a[i] += 32;
for (int i = 0; b[i]; i ++ )
if (b[i] >= 'A' && b[i] <= 'Z')
b[i] += 32;
int t = strcmp(a, b);
if (t == 0) puts("=");
else if (t < 0) puts("<");
else puts(">");
return 0;
}
3.去掉多余的空格
//cin做法
#include <iostream>
using namespace std;
int main()
{
string s;
while (cin >> s) cout << s << ' ' ;
return 0;
}
//第一类双指针算法
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin, s);
string r;
for (int i = 0; i < s.size(); i ++ )
if (s[i] != ' ') r += s[i];
else
{
r += ' ';
int j = i;
while (j < s.size() && s[j] == ' ') j ++ ;
i = j - 1;
}
cout << r << endl;
return 0;
}
//局部性判断方法
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin, s);
string r;
for (int i = 0; i < s.size(); i ++ )
if (s[i] != ' ') r += s[i];
else
{
if (!i || s[i - 1] != ' ') r += ' ';
}
cout << r << endl;
return 0;
}
4.信息加密
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin, s);
for (auto &c : s)
if (c >= 'a' && c <= 'z') c = (c - 'a' + 1) % 26 + 'a';
else if (c >= 'A' && c <= 'Z') c = (c - 'A' + 1) % 26 + 'A';
cout << s << endl;
return 0;
}
5.输出字符串
#include <iostream>
using namespace std;
int main()
{
string a, b;
getline(cin, a);
for (int i = 0; i < a.size(); i ++ ) b += a[i] + a[(i + 1) % a.size()];
cout << b << endl;
return 0;
}
6.单词替换
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string s, a, b;
getline(cin, s);
cin >> a >> b;
stringstream ssin(s);
string str;
while (ssin >> str)
if (str == a) cout << b << ' ';
else cout << str << ' ';
return 0;
}
**7.字符串中最长的连续出现的字符 **
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while (n -- )
{
string str;
cin >> str;
int cnt = 0;
char c;
for (int i = 0; i < str.size(); i ++ )
{
int j = i;
while (j < str.size() && str[j] == str[i]) j ++ ;
if (j - i > cnt) cnt = j - i, c = str[i];
i = j - 1;
}
cout << c << ' ' << cnt << endl;
}
return 0;
}
8.最长单词
#include <iostream>
using namespace std;
int main()
{
string res, str;
while (cin >> str)
{
if (str.back() == '.') str.pop_back();
if (str.size() > res.size()) res = str;
}
cout << res << endl;
return 0;
}
9.倒排单词
#include <iostream>
using namespace std;
int main()
{
string str[100];
int n = 0;
while (cin >> str[n]) n ++ ;
for (int i = n - 1; i >= 0; i -- ) cout << str[i] << ' ';
cout << endl;
return 0;
}
10.字符串移位包含问题
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string a, b;
cin >> a >> b;
if (a.size() < b.size()) swap(a, b);
for (int i = 0; i < a.size(); i ++ )
{
a = a.substr(1) + a[0];
for (int j = 0; j + b.size() <= a.size(); j ++ )
{
int k = 0;
for (; k < b.size(); k ++ )
if (a[j + k] != b[k])
break;
if (k == b.size())
{
puts("true");
return 0;
}
}
}
puts("false");
return 0;
}
**11.字符串乘方 **
#include <iostream>
using namespace std;
int main()
{
string str;
while (cin >> str, str != ".")
{
int len = str.size();
for (int n = len; n; n -- )
if (len % n == 0)
{
int m = len / n;
string s = str.substr(0, m);
string r;
for (int i = 0; i < n; i ++ ) r += s;
if (r == str)
{
cout << n << endl;
break;
}
}
}
return 0;
}
12.字符串最大跨距
#include <iostream>
using namespace std;
int main()
{
string s, s1, s2;
char c;
while (cin >> c, c != ',') s += c;
while (cin >> c, c != ',') s1 += c;
while (cin >> c) s2 += c;
if (s.size() < s1.size() || s.size() < s2.size()) puts("-1");
else
{
int l = 0;
while (l + s1.size() <= s.size())
{
int k = 0;
while (k < s1.size())
{
if (s[l + k] != s1[k]) break;
k ++ ;
}
if (k == s1.size()) break;
l ++ ;
}
int r = s.size() - s2.size();
while (r >= 0)
{
int k = 0;
while (k < s2.size())
{
if (s[r + k] != s2[k]) break;
k ++ ;
}
if (k == s2.size()) break;
r -- ;
}
l += s1.size() - 1;
if (l >= r) puts("-1");
else printf("%d\n", r - l - 1);
}
return 0;
}
13.最长公共字符串后缀
#include <iostream>
using namespace std;
const int N = 200;
int n;
string str[N];
int main()
{
while (cin >> n, n)
{
int len = 1000;
for (int i = 0; i < n; i ++ )
{
cin >> str[i];
if (len > str[i].size()) len = str[i].size();
}
while (len)
{
bool success = true;
for (int i = 1; i < n; i ++ )
{
bool is_same = true;
for (int j = 1; j <= len; j ++ )
if (str[0][str[0].size() - j] != str[i][str[i].size() - j])
{
is_same = false;
break;
}
if (!is_same)
{
success = false;
break;
}
}
if (success) break;
len -- ;
}
cout << str[0].substr(str[0].size() - len) << endl;
}
return 0;
}
学习资源 《语法基础》