当前位置:网站首页>字符串处理:
字符串处理:
2022-06-12 02:55:00 【HENU_Lzey】
字符串处理:
1.字符串的输入
a.cin.getline(字符型数组名称,输入的位数,截止标志);
例
#include<iostream>
using namespace std;
char q[11100];
int main(){
cin.getline(q,10,'a');//对q进行输入到a截止且最多输入10位;
for(int i=0;i<11100;i++){
cout<<q[i];
}
return 0;
}
结果:
b.getline(cin,字符串名称)//遇见换行键终止输入;
#include<iostream>
#include<cstring>
using namespace std;
string a;
int main(){
getline(cin,a);//对字符串进行赋值。
cout<<a;
return 0;
}
c.cin//遇见空格即停止输入;用于做遇空格输入停止的题;
#include<iostream>
#include<cstring>
using namespace std;
string a;
int main(){
cin>>a;
cout<<a;
return 0;
}
结果:
d.gets()也用于c++;
2.常用函数:
a.查找:find()函数;
例:
int a=s1.find(q);//在字符串s1中顺序查找q并返回q的首地址给a;
int a=s1.rfind(q);//在字符串s1中倒叙查找q并返回q的地址给a;
int a=s1.find(q,1);//在字符串s1的第一个字符后顺序查找q并返回q的首地址给a;
b.输出处理:substr()函数;
例:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str.substr(3)<<endl; //从第三个位置开始输出字符;
cout<<str.substr(2,4)<<endl;//从第二个开始输出四个字符;
return 0;
}
例:
结果:
c.插入:insert()函数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin>>a;
cout<<a.insert(6,"henu")<<endl;//从第6个位置后插入“henu”
return 0;
}
结果:
d.追加:a.append(“henu”);
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin>>a;
cout<<"在输入字符串的尾部添加字符串a:"<<a.append("henu")<<endl;
结果:
e.交换字符a.swap(b);//字符a,b的交换;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a,b;
cin>>a>>b;
cout<<"交换前:<<"str1="<<str1<<" "<<"str2="<<str2<<endl;
str1.swap(str2);
cout<<"交换后:"<<"str1="<<str1<<" "<<"str2="<<str2<<endl;
return 0;
}

f.字符串大小:size()函数和length()函数;//sizeof()用于char型数组的长度求解;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin>>a;
cout<<a.size()<<endl;
cout<<a.length();
return 0;
}
结果:
g.字符串大小的比较:compare()
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a,b;
cin>>a>>b;
cout<<"字符串比较结果:"<<a.compare(b)<<endl;
return 0;
}



综上 a.compare(b)
若a>b则返回-1;
若a<b则返回1;
若a==b则返回0;
h.另外基本的函数
strcpy(s1,s2) :复制字符串s2到s1
strcat(s1,s2) :连接s2到s1的末尾
strlen(s1) :返回字符串s1的长度
strcmp(s1,s2) :若s1和s2是相同的,则返回0,s1< s2,返回值小于0,若s1>s2,返回值大于0
例题:
#include<iostream>
#include<string>
using namespace std;
char zf[210];
int main()
{
cin.getline(zf,210);
int cnt=0;
int len=sizeof(zf);
for(int i=0;i<len;i++){
if(zf[i]=='a'||zf[i]=='d'||zf[i]=='g'||zf[i]=='j'||zf[i]=='m'||zf[i]=='p'||zf[i]=='t'||zf[i]=='w'||zf[i]==' '){
cnt++;
}else if(zf[i]=='b'||zf[i]=='e'||zf[i]=='h'||zf[i]=='k'||zf[i]=='n'||zf[i]=='q'||zf[i]=='u'||zf[i]=='x'){
cnt+=2;
}else if(zf[i]=='c'||zf[i]=='f'||zf[i]=='i'||zf[i]=='l'||zf[i]=='o'||zf[i]=='r'||zf[i]=='v'||zf[i]=='y'){
cnt+=3;
}else if(zf[i]=='s'||zf[i]=='z'){
cnt+=4;
}
}cout<<cnt;
return 0;
}
边栏推荐
- oracle之模式对象
- Android HTML5 page load cache optimization
- How to reduce the complexity of cloud computing infrastructure?
- For the first time, why not choose "pure medium platform" for byte beating data platform
- 跨域有哪些解决方法?
- errno: -4091, syscall: ‘listen‘, address: ‘::‘, port: 8000
- El upload upload file
- Layered architecture of DDD
- Demand and business model innovation - demand 9- prototype
- Machine learning - dimensionality reduction (data compression, data visualization)
猜你喜欢

The program actively carries out telephone short message alarm, and customizes telephone, short message and nail alarm notifications

Inverted string - two solutions
![[point cloud compression] variable image compression with a scale hyperprior](/img/d4/4084f64d20c8e622cddef2310d3b6c.png)
[point cloud compression] variable image compression with a scale hyperprior

Maya Front Office Rendering plug - in Mel script Tool

In 2022, don't you know the difference between arrow function and ordinary function?

RPC 入门

Demand and business model innovation - demand 11 - overview of demand analysis

Apache simple honeypot

【点云压缩】Variational Image Compression with A Scale Hyperprior

Unity3d ugui translucent or linear gradient pictures display abnormally (blurred) problem solving (color space mismatch)
随机推荐
Calculus review 2
Force deduction solution summary 1022- sum of binary numbers from root to leaf
Force deduction solution summary 732- my schedule III
跨域有哪些解决方法?
Requirements and business model innovation - Requirements 7- user requirements acquisition based on use case / scenario model
Oracle sequence
How to build urban smart bus travel? Quick code to answer
One article to show you how to understand the harmonyos application on the shelves
【高代码文件格式API】道宁为您提供文件格式API集——Aspose,只需几行代码即可创建转换和操作100多种文件格式
A single quarter of educational technology revenue of 230million: a year-on-year decrease of 51% and a sharp narrowing of net loss
Ue4\ue5 touch screen touch event: single finger and double finger
Geforce GTX 2050/2080/3090/a6000 auto install NVIDIA graphics driver
laravel 8 选用 jwt 进行接口验证
Selection (044) - what is the output of the following code?
Package a mobile APK app containing watch app - ticwear
Don't rush to work after the college entrance examination. There are plenty of opportunities after working. It's not bad for these three months
Application of ankery anti shake electric products in a chemical project in Hebei
Start ticwatch2
Abaqus中批量对节点施加集中力荷载
Unique paths for leetcode topic resolution