当前位置:网站首页>string manipulation:
string manipulation:
2022-06-12 03:03:00 【HENU_ Lzey】
string manipulation :
1. Input of string
a.cin.getline( Character array name , The number of digits entered , Cut off flag );
example
#include<iostream>
using namespace std;
char q[11100];
int main(){
cin.getline(q,10,'a');// Yes q Enter into a Cut off and enter at most 10 position ;
for(int i=0;i<11100;i++){
cout<<q[i];
}
return 0;
}
result :
b.getline(cin, String name )// When the line feed key is encountered, the input is terminated ;
#include<iostream>
#include<cstring>
using namespace std;
string a;
int main(){
getline(cin,a);// Assign a value to a string .
cout<<a;
return 0;
}
c.cin// Stop typing when a space is encountered ; It is used to do the question that the input stops when encountering a space ;
#include<iostream>
#include<cstring>
using namespace std;
string a;
int main(){
cin>>a;
cout<<a;
return 0;
}
result :
d.gets() Also used in c++;
2. Common functions :
a. lookup :find() function ;
example :
int a=s1.find(q);// In string s1 Find in order q And back to q First address to a;
int a=s1.rfind(q);// In string s1 Flashback search in q And back to q The address to a;
int a=s1.find(q,1);// In string s1 Search in sequence after the first character of q And back to q First address to a;
b. Output processing :substr() function ;
example :
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str.substr(3)<<endl; // Output characters from the third position ;
cout<<str.substr(2,4)<<endl;// Output four characters starting from the second ;
return 0;
}
example :
result :
c. Insert :insert() function
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin>>a;
cout<<a.insert(6,"henu")<<endl;// From 6 Insert after two positions “henu”
return 0;
}
result :
d. Additional :a.append(“henu”);
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin>>a;
cout<<" Add a string at the end of the input string a:"<<a.append("henu")<<endl;
result :
e. Exchange characters a.swap(b);// character a,b In exchange for ;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a,b;
cin>>a>>b;
cout<<" Exchange before :<<"str1="<<str1<<" "<<"str2="<<str2<<endl;
str1.swap(str2);
cout<<" After exchanging :"<<"str1="<<str1<<" "<<"str2="<<str2<<endl;
return 0;
}

f. String size :size() Functions and length() function ;//sizeof() be used for char Type array length solution ;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cin>>a;
cout<<a.size()<<endl;
cout<<a.length();
return 0;
}
result :
g. Comparison of string size :compare()
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a,b;
cin>>a>>b;
cout<<" String comparison results :"<<a.compare(b)<<endl;
return 0;
}



Sum up a.compare(b)
if a>b Then return to -1;
if a<b Then return to 1;
if a==b Then return to 0;
h. Other basic functions
strcpy(s1,s2) : Copy string s2 To s1
strcat(s1,s2) : Connect s2 To s1 At the end of
strlen(s1) : Return string s1 The length of
strcmp(s1,s2) : if s1 and s2 It's the same , Then return to 0,s1< s2, Return value less than 0, if s1>s2, Return value greater than 0
Example :
#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;
}
边栏推荐
- Android HTML5 page load cache optimization
- Kubernetes affinity learning notes
- One article to show you how to understand the harmonyos application on the shelves
- Kubernetes' learning path. Is there any "easy mode" Q recommendation for container hybrid cloud
- [DFS "want" or "don't"] seek subsets; Seeking combination
- Requirements and business model analysis requirements 13 data modeling
- min25筛
- oracle之用户和表空间
- Hypergraph tilted data is merged into root node and transferred to 3dfiles
- Yu Xia looks at win system kernel -- debugging
猜你喜欢

SSH public key login failed with error: Sign_ and_ send_ pubkey: no mutual signature supported

Wechat applet project example - renju for two

Recommend 6 office software, easy to use and free, double the efficiency

一起教育科技单季营收2.3亿:同比降51% 净亏大幅收窄

cupp字典生成工具(同类工具还有crunch)

无限循环判断方法;

Application of residual pressure monitoring system in high-rise civil buildings

Requirements and business model innovation - Requirements 12 - process oriented modeling

errno: -4078, code: ‘ECONNREFUSED‘, syscall: ‘connect‘, address: ‘127.0.0.1‘, port: 3306;postman报错

Wechat applet project example - Fitness calculator
随机推荐
Kubernetes affinity learning notes
In 2022, don't you know the difference between arrow function and ordinary function?
Interpreting Julia's 2021: step by step towards the mainstream programming language
Query the duplicate values of multiple fields in the database, output the number, and add them.
利用ssh公钥传输文件
[DFS "want" or "don't"] seek subsets; Seeking combination
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
The market value has exceeded $3trillion. Why should apple, which has been criticized as a loser, rise again and again?
What is the commonly heard sub table of MySQL? Why does MySQL need tables?
One article to show you how to understand the harmonyos application on the shelves
How to make div 100% page (not screen) height- How to make a div 100% of page (not screen) height?
What is the core of Web3?
[digital signal processing] correlation function (finite signal | autocorrelation function of finite signal)
Application of acrelcloud-6000 secure power cloud platform in a commercial plaza
Comment prévenir les incendies électriques dans les centres commerciaux?
Common errors when mysql8 connects through JDBC
Laravel 8 selects JWT for interface verification
Comparaison de la taille des fractions
Demand and business model innovation - demand 10- observation and document review
Demand and business model innovation - demand 6- stakeholder analysis and hard sampling