字符串—C++

string 常见函数
(1)string的查找和截取

■ find   (子串substr): 查找子字符串第一次出现的下标,没有返回string::npos  ( 注意判断其为-1);
■ find(substr,x):  在字符串的下标x 之后,查找子串substr;
■ substr  (开始位置i,  子串长度len):      截取子字符串,当len> 字符串长度的时候,只取剩余的;
■ substr    (开始位置i):      截取子字符串,从下标为i  开始截取到最后;
(2)string 的删除、插入和替换 相 关 函 数
■  erase(开始下标i, 删除长度len): 删除字符串第i 个下标开始的len个字符;
■  erase ( 开始下标i):  删 除 字 符 串 第i 个下标开始往后的所有字符;
■  insert (插入下标,插入字符串s):  在 字 符 串 下 标 为i 的位置插入 一 个字符串s;
■ replace(i,len,str): 从下标为i 开始,替换len个字符为str
(3)字符类型判断/转换,数组/string排序相关函数
■   字符类型判断函数:(非string 函数) 
√  isalpha©:  判 断c 是否为字母
√  islower©:  判断是否为小写 
√  isupper©:   判断是否为大写
√  isdigit©:    判断是否为数字   说 明 :返回非0表示真,返回0表示假;
■  字符类型转换函数:(非string 函数) 
√  tolower©:   字符转小写
√ toupper©:   字符转大写 说明:  返 回i n t   ;
■  排序和倒序函数(非string 函 数 )
√  sort  (起始地址,结束地址+1): 数组升序排序 
√ reverse  (起始地址,结束地址+1): 数组逆序
■  获取头尾指针
√  s.begin():       获取字符串s  的头位置(指针)
√ s.end():           获取字符串s  的尾位置(最后一个字符后面的位置)(指针)
注意:数组的本质是数组中下标为0的元素的地址;(首元素的地址)

例题
1.打印小写字母表(基础)

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
for (char c = 'a'; c <= 'z'; c++)
{
cout << c << endl;
}
return 0;
}

2.判断是否构成回文(基础)
方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n / 2; i++)
{
if (s[i] != s[n - 1 - i])
{
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}

方法二:

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 <bits/stdc++.h>
using namespace std;

bool huiwen(string s1) {
string s2 = s1;
reverse(s2.begin(), s2.end());
return s1 == s2;
}

int main() {
string s, s1;
cin >> s;

for (int i = 0; i < s.size(); i++) {
if (s[i] != '.')
s1 += s[i];
}

if (huiwen(s1))
cout << "TRUE";
else
cout << "FALSE";

return 0;
}

3.字符串加密(入门)
描述
在情报传递过程中,为了防止情报被截获,往往需要对情报用一定的方式加密,简单的加密算法虽然不足以完全避免情报被破译,但仍然能防止情报被轻易的识别。

我们给出一种最简的的加密方法,对给定的一个字符串,把其中从
a−y ,A−Y 的字母用其后继字母替代,把 z 和 Z 用 a 和 A 替代,其他非字母字符不变,则可得到一个简单的加密字符串。
输入描述
输入一行,包含一个字符串,长度不超过 80 个字符。
输出描述
输出每行字符串的加密字符串。
用例输入1
Hello! How are you!
用例输出1
Ifmmp! Ipx bsf zpv!
参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin, s);
for (int i = 0; i < s.size(); i++)
{
if (isalpha(s[i]))
{
if (s[i] == 'z')
s[i] = 'a';
else if (s[i] == 'Z')
s[i] = 'A';
else
s[i]++;
}
}
cout << s << endl;
return 0;
}

4.求子串的位置(入门)
描述
请问在一个父字符串 s 中是否存在子字符串 t 。如果存在,则输出子字符串 t 在父字符串中所有的起始位置,如果不存在,则输出 -1 。
比如:假设父字符串 s=Go Abc good goole,子字符串 t=go ,那么输出位置:
8
13
说明
请分别用 find 及 substr 两种方法求解。
输入描述
第一行输入父字符串的值(字符串长度不超过 100)。
第二行输入子字符串的值(子字符串长度不超过 100 )。
输出描述
输出子字符串在父字符串中所有的位置,如果父字符串中不存在子字符串,请输出 -1 。
用例输入1
Go Abc good goole!
go
用例输出1
8
13
用例输入2
Go Abc good goole!
Good
用例输出2
-1
参考代码
方法一:(find)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main()
{
string s, t;
getline(cin, s);
getline(cin, t);
int pos = s.find(t);
while (pos != string::npos)
{
cout << pos << endl;
pos = s.find(t, pos + 1);
}
if (pos == string::npos)
cout << -1 << endl;
return 0;
}

方法二:(substr)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main()
{
string s, t;
getline(cin, s);
getline(cin, t);
int n = s.size(), m = t.size();
for (int i = 0; i <= n - m; i++)
{
if (s.substr(i, m) == t)
cout << i << endl;
}
if (n < m)
cout << -1 << endl;
return 0;
}

5.计算表达式(提升)
描述
表达式的形式如:3+5∗6−4 其中, 运算数为一位整数,运算符为
+、−、∗三种,且运算符没有优先级的区分,一律自左向右计算。 如上例的计算过程为:3+5∗6−4=8∗6−4=48−4=44
输入描述
一行,即表达式字符串。(长度小于 100)
输出描述
一个整数,即表达式的计算结果(结果在−20000至20000之间)
用例输入1
3+5*6-4
用例输出1
44
参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{
string s;
cin >> s;
int n = s.size();
int res = s[0] - '0';
for (int i = 1; i < n; i += 2)
{
if (s[i] == '+')
res += s[i + 1] - '0';
else if (s[i] == '-')
res -= s[i + 1] - '0';
else if (s[i] == '*')
res *= s[i + 1] - '0';
}
cout << res << endl;
return 0;
}