当前位置:网站首页>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 :
 Insert picture description here
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 :
 Insert picture description here
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 :
 Insert picture description here
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 :
 Insert picture description here
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 :
 Insert picture description here
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;
}

 Insert picture description here
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 :
 Insert picture description here
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;
}

 Insert picture description here
 Insert picture description here
 Insert picture description here

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 :
 Insert picture description here

#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;
}
原网站

版权声明
本文为[HENU_ Lzey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120255312891.html