小樽C++ 单章④ 字符数组与字符串

目录

一、字符与数组

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循环输入。这是跟一维数组不同的特点!


1.求字符数组的长度

通过'\0'代表字符串结束,那循环判断条件用'\0'。

看到上面代码,求个字符串长度这么麻烦,那有什么简单的方法?

导入库<cstring>,调用 strlen()函数,即可获取字符串长度。

#include <iostream>
#include <cstring>
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=5
    
    int len2 = strlen(s);
	cout<<"长度2:"<<len2;
    return 0;
}


2.查找单词

 

在字符串中找某一个单词出现的次数,例如 字符串hihellohi, "hi" 出现了2次。

#include <iostream>
#include <cstring>
using namespace std;
int main() {
    char s[] = {"hi"};
    char a[1000];
    int ans = 0;
    cout<<"请输入字符串:"; 
    cin >> a; // hihellohi
    int len=strlen(a);
    for(int i=0;i<len-1;i++){//选取开始对比位置
        bool flag = true;
        for(int j=0;j<2;j++){   //从第a[i]个开始对比
            if(a[i+j]!=s[j]){       //发现没有匹配上
                flag = false;       //标记没配对上
                break;              //结束循环
            }
        }
        if(flag == true){
            ans++;
        }
    }
    cout <<"匹配相等有:"<< ans << endl;
    return 0;
}


二、字符串与数组

1.字符数组定义:string s;

2.字符数组赋值:string b = “hello”;

b[0] =        b[1] =        b[2] =        b[3] =        

3.字符串长度:字符数组类似,导入库<string>,用变量名.size(),即可。

例如:s.size()

2.1 字符串倒序输出

例如输入 “hello”,倒序输出就是:“olleh”。

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s;
	cin>>s;
	cout<<"长度:"<<s.size()<<endl; 
	for(int i=s.size()-1;i>=0;i--){
		cout<<s[i];
	} 
	return 0; 
} 

2.2 字符串比较

规则:两个字符串从左往右看;先比首字符,通过ASCII码比大小,如果两个字符串首字母不一致,那哪个字符大就代表哪个字符串大! 跟字符串长度无关。当同位置的字符一致时,继续往后比,知道比完,再看哪个字符串长度长。

例如:

Abc 与 Abd        哪个大?        

Abcd 与 Bab       哪个大?        

Abcd 与 Abcde     哪个大?        

请输入两个字符串s1,s2; 比较两个字符串的大小,如果s1>s2就输出>, 否则如果s1=s2就输出=,否则就输出<。而且要实现可重复不断地比较!

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s1, s2;
    while(1){
        cin>>s1>>s2;
        if(s1>s2){
            cout<<s1<<'>'<<s2<<endl;
        }if(s1<s2){
            cout<<s1<<'<'<<s2<<endl;
        } if(s1==s2){
            cout<<s1<<'='<<s2<<endl;
        }
    }
    return 0;
}

2.3 大写字母输出

输入一个字符串,把字符串中大写的字母输出来。

#include <iostream>
#include <string>
using namespace std;
int main(){
//	输入字符串s1,有大小写,只输出大写字母
	string s1;
	cin>>s1;
	for(int i=0;i<s1.size();i++){
		if(s1[i]>='A' && s1[i]<='Z'){
			cout<<s1[i]<<" ";
		}
	} 
	return 0;
}

整行读取 与 拼接字符串

getline(), 整行(有空格)读取字符串。

我们知道输入字符串,空格隔开,再输入字符串是代表输入两个字符串。但如果我们字符串本身就要输入空格怎么办?

格式:getline(cin,变量);

使用+拼接字符串 s1 + s2。

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(){
	string s;
	getline(cin,s);
	string s2="I am good";
	cout<<endl<<s+s2; 
}


三、字符串常用函数

一、初始化字符串:

  1. 导入库 include<string>
  2. 定义与赋值

定义一个字符串:string s = “hello jxz”;

二、字符串操作:(增删改查)

查找操作:find函数查找

格式:变量名.find("查词"),有就返回一个"查词"的索引位置。

如果find函数没有找到会返回string::npos,可以通过if判断有无找到。

#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	cin>>s; // hihellohello 
//	字符串的增删改查
//	 用法:变量名.find("查"),返回一个索引 
	cout<<s.find("he")<<endl; 	//输出索引位置 
	cout<<s.find("llo")<<endl;  //输出索引位置
	
	if(s.find("hllo") != string::npos){ //找得到的意思 
		cout<<s.find("hllo");
	}
	else{
		cout<<"找不到"; 
	}
    return 0;
}

插入操作:变量名.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<<s[0].find(s[1]);
	}
	else{
		cout<<"NO!";
	}

 练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<<names[i]<<" ";
	}

练3:数字签名是对文本进行加密的一种方式,我们接下来要运用学到的字符串的插入和查找函数,对一串文本进行加密。这一串文本如果出现数字66,则在第一个66之前插入“jiami”;否则就将文本原本输出。

//数字加密

    string text;
    cin >> text;
    if(text.find("66") != string::npos){
    	cout<<text.insert(text.find("66"),"jiami")<<endl;
	}
	else{
		cout<<text;
	}


三、截取与替换字符串

截取一小段的字符串: s.substr(位置,长度)

定义字符串 string s = "eat good!"; 我们只要字符串的一部分good!

cout<<s.substr(4);

cout<<s.substr(0,3); //我们只要获取eat  3的位置是截取的长度

练4:对上面的名字字符串操作。在这里,我们希望把每一个读入的字符串的第2到第6个字符截取并输出。请在读入每个字符串后,将每个字符串赋值为截取后的结果。

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;
}


四、替换字符串的某值

 s.replace(位置,长度,"替换值");

练5:接着把上面的截取操作变成替换操作;把字符串的第2到第6个字符替换为“ABC”。


   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;
}

练6:字符串 s = "13sb344sbhsbjksbk",输出要消除空格。结果:13344hjkk

  1. 1. 思路:定义完变量s,就循环:满足s.find(" ") != string::npos 执行程序
  2. 2. 用替换replace() 把空格" "替换为""即可。最后循环结束,输出字符串s。
    string s = "13 344 h jk k";
	for(int i=0;s.find(" ") != string::npos;i++){
		s = s.replace(s.find(" "),1,"");
	}
	cout<<s;
	return 0;

四、应用题

1.后缀

#include<iostream>
using namespace std;
int main(){
	string s;
	cin>>s;
	int len = s.size();
	if(s[len-2]=='e' && s[len-1]=='r'){
		for(int i=0;i<len-2;i++){
			cout<<s[i];
		}
	}
	else if(s[len-2]=='l' && s[len-1]=='y'){
		for(int i=0;i<len-2;i++){
			cout<<s[i];
		}
	}
	else if(s[len-3]=='i' && s[len-2]=='n' && s[len-1]=='g'){
		for(int i=0;i<len-3;i++){
			cout<<s[i];
		}
	}
	else{
		cout<<s;
	}
	return 0;
}


2.开学消消乐

 

 

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s;
    cin >> s;
    for(int i=0;i<s.size();i++){
        string a = s.substr(i,3);
        if(a[0]==a[1]&&a[1]==a[2]){
            //cout<<a<<endl;
            s.replace(i,3,"");
            i=-1;
        }
    }
    cout<<s;
}

 

3.找最长最短单词

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s;
    cin >> s;
    for(int i=0;i<s.size();i++){
        string a = s.substr(i,3);
        if(a[0]==a[1]&&a[1]==a[2]){
            //cout<<a<<endl;
            s.replace(i,3,"");
            i=-1;
        }
    }
    cout<<s;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐樽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值