目录
一、字符与数组
1.求字符数组的长度
2.查找单词
二、字符串与数组
2.1 字符串倒序输出
2.2 字符串比较
2.3 大写字母输出
编辑
三、字符串常用函数
一、初始化字符串:
二、字符串操作:(增删改查)
三、截取与替换字符串
四、替换字符串的某值
四、应用题
1.后缀
2.开学消消乐
3.找最长最短单词
字符串:由多个字符构成的连续成一串就是字符串。
注意区分字符'a' 和字符串"a";字符是单引号,字符串是双引号。
字符是指单个的字符,字符串由任意数量的字符,比如"abc"、 "a 8A"
如果我们将一个字符串拆开:
例如:"hello" 拆: 'h','e','1','1','o','\0'
这里一定要注意,所有字符串末尾,都会由系统自动添加一个'\0' 作为结尾,也就是说一个长度为 5的字符串,实际需要占用6个单元空间。
1.字符数组定义:char a[100];
2.字符数组赋值:char b[100] ="hello";
b[0] = b[1] = b[2] = b[3] =
字符数组输入的可以直接用cin输入值,可以不用for循环输入。这是跟一维数组不同的特点!
通过'\0'代表字符串结束,那循环判断条件用'\0'。
看到上面代码,求个字符串长度这么麻烦,那有什么简单的方法?
导入库
#include
#include
using namespace std;
int main() {char s[1000]; int len = 0;//int整数 cin >> s;//s="hello",以'\0'结尾 for(int i=0; s[i] != '\0'; i++){//s[0]='h',s[1]='e'len++;}cout << len << endl;//len=5int len2 = strlen(s);cout<<"长度2:"<
在字符串中找某一个单词出现的次数,例如 字符串hihellohi, "hi" 出现了2次。
#include
#include
using namespace std;
int main() {char s[] = {"hi"};char a[1000];int ans = 0;cout<<"请输入字符串:"; cin >> a; // hihellohiint len=strlen(a);for(int i=0;i
1.字符数组定义:string s;
2.字符数组赋值:string b = “hello”;
b[0] = b[1] = b[2] = b[3] =
3.字符串长度:字符数组类似,导入库
例如:s.size()
例如输入 “hello”,倒序输出就是:“olleh”。
#include
#include
using namespace std;
int main() {string s;cin>>s;cout<<"长度:"<=0;i--){cout<
规则:两个字符串从左往右看;先比首字符,通过ASCII码比大小,如果两个字符串首字母不一致,那哪个字符大就代表哪个字符串大! 跟字符串长度无关。当同位置的字符一致时,继续往后比,知道比完,再看哪个字符串长度长。
例如:
Abc 与 Abd 哪个大?
Abcd 与 Bab 哪个大?
Abcd 与 Abcde 哪个大?
请输入两个字符串s1,s2; 比较两个字符串的大小,如果s1>s2就输出>, 否则如果s1=s2就输出=,否则就输出<。而且要实现可重复不断地比较!
#include
#include
using namespace std;
int main() {string s1, s2;while(1){cin>>s1>>s2;if(s1>s2){cout<'<
输入一个字符串,把字符串中大写的字母输出来。
#include
#include
using namespace std;
int main(){
// 输入字符串s1,有大小写,只输出大写字母string s1;cin>>s1;for(int i=0;i='A' && s1[i]<='Z'){cout<
整行读取 与 拼接字符串
getline(), 整行(有空格)读取字符串。
我们知道输入字符串,空格隔开,再输入字符串是代表输入两个字符串。但如果我们字符串本身就要输入空格怎么办?
格式:getline(cin,变量);
使用+拼接字符串 s1 + s2。
#include
#include
#include
using namespace std;
int main(){string s;getline(cin,s);string s2="I am good";cout<
定义一个字符串:string s = “hello jxz”;
查找操作:find函数查找
格式:变量名.find("查词"),有就返回一个"查词"的索引位置。
如果find函数没有找到会返回string::npos,可以通过if判断有无找到。
#include
#include
using namespace std;
int main(){string s;cin>>s; // hihellohello
// 字符串的增删改查
// 用法:变量名.find("查"),返回一个索引 cout<
插入操作:变量名.insert(索引,值)
cout << s.insert(2,“hi”); //索引2位置插入
s.insert(0, "before"); // 首位置插入
s.insert(s.size(), "end"); // 末尾位置插入
练1:定义字符串数组s[2];输入第一个、第二个字符串。
查找,第一个字符串s[0]是否包含s[1]第二个字符串,有就输出该位置,否则输出NO。
string s[2];cin>>s[0]>>s[1];if(s[0].find(s[1]) != string::npos){cout<
练2:定义声明存储3个字符串姓名的数组,循环输入值;在它的第0个位置上加上字符串Mr. ,最后把最新的结果输出出来。
string names[3];for(int i=0;i<3;i++){cin>>names[i];names[i].insert(0,"Mr. ");} for(int i=0;i<3;i++){cout<
练3:数字签名是对文本进行加密的一种方式,我们接下来要运用学到的字符串的插入和查找函数,对一串文本进行加密。这一串文本如果出现数字66,则在第一个66之前插入“jiami”;否则就将文本原本输出。
//数字加密
string text;cin >> text;if(text.find("66") != string::npos){cout<
截取一小段的字符串: s.substr(位置,长度)
定义字符串 string s = "eat good!"; 我们只要字符串的一部分good!
cout< cout< 练4:对上面的名字字符串操作。在这里,我们希望把每一个读入的字符串的第2到第6个字符截取并输出。请在读入每个字符串后,将每个字符串赋值为截取后的结果。 s.replace(位置,长度,"替换值"); 练5:接着把上面的截取操作变成替换操作;把字符串的第2到第6个字符替换为“ABC”。 练6:字符串 s = "13sb344sbhsbjksbk",输出要消除空格。结果:13344hjkk string names[3];
for (int i = 0; i < 3; i++) {cin >> names[i];names[i]=names[i].substr(1,5);}for (int i = 0; i < 3; i++) {cout << names[i] << endl;
}
四、替换字符串的某值
string names[3];for (int i = 0; i < 3; i++) {cin >> names[i];names[i].replace(1,5,"ABC");}for (int i = 0; i < 3; i++) {cout << names[i] << endl;
}
string s = "13 344 h jk k";for(int i=0;s.find(" ") != string::npos;i++){s = s.replace(s.find(" "),1,"");}cout<
四、应用题
1.后缀
#include
2.开学消消乐
#include
3.找最长最短单词
#include