String Title Collection (I)
ps: I will brush all the string titles on Luogu and acwing, and send them up, middle and down. This blog is rough. I'll add some knowledge when I wake up tomorrow
As for the original intention of reviewing the string, I still can't grasp it well, and the string is a necessary test in the school competition, so I hope to brush all the questions of the string before the school competition (at least it won't explode hhh)
1. Length of string
subject
Given a non empty string with a length of no more than 100, please find its specific length.
Input format
Enter a line representing a string. Note that the string may contain spaces.
Output format
Outputs an integer representing its length.
Input example:
I love Beijing.
Output example:
15
AC code
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { char s[101]; fgets(s,101,stdin); //Indicates how many characters are read int len=0; for(int i=0;s[i]&&s[i]!='\n';i++) {//Because fgets will read the carriage return, write s [i]! =\ n' len++; } printf("%d\n",len); return 0; }
Note: about fgets(), I feel that the blogger wrote well Detailed explanation of fgets written by the boss (I'm a porter)
2. Number of digits in the string
subject
Enter a line of characters with a length of no more than 100. Please count the number of numeric characters.
Input format
Enter a line of characters. Note that it may contain spaces.
Output format
Output an integer representing the number of numeric characters.
Input example:
I am 18 years old this year.
Output example:
2
AC code
#include<iostream> #include<cstdio> using namespace std; int main() { char c; int nums=0; while(cin>>c) { if(c>='0'&&c<='9') nums++; } printf("%d\n",nums); return 0; }
3. Cyclic phase restriction
subject
Circular phase kering is a small game played by two people.
The order word is "hunter, bear and gun". They say the order word at the same time and make an action at the same time - the action of the hunter is to put their hands on their hips; The action of a bear is to put his hands on his chest; The action of the gun is to raise both hands in the shape of a pistol.
The two sides decide whether to win or lose by this action. If the hunter wins the gun, the gun wins the bear, and the bear wins the hunter, if the action is the same, it will be regarded as a draw.
Now, given a series of action combinations, please judge the result of the game.
Input format
The first row contains the integer T, indicating that there are t groups of test data.
Next, line T, each line contains two strings representing the actions made by two people in a game. The string is one of hunter, bear and gun. These three words represent hunter, bear and gun respectively.
Output format
If the first player wins, Player1 is output.
If the second player wins, Player2 is output.
If there is a draw, Tie is output.
Data range
1 ≤ N ≤ 100
sample input
3 Hunter Gun Bear Bear Hunter Bear
sample output
Player1 Tie Player2
AC code
#include<iostream> #include<cstdio> using namespace std; int main() { int n; cin>>n; for(int i=0;i<n;i++) { string a,b; cin>>a>>b; int x,y; if(a=="Hunter") { x=0; } else if(a=="Bear") { x=1; } else x=2; if(b=="Hunter") { y=0; } else if(b=="Bear") { y=1; } else y=2; if(x==y) { puts("Tie"); } else if (x == (y + 1) % 3) { puts("Player1"); } else puts("Player2"); } return 0; }
4. String plus space
subject
Given a string, add a space between each character of the string.
Output the modified new string.
Input format
A line containing a string. Note that the string may contain spaces.
Output format
Output the string after adding spaces.
Data range
1 ≤ string length ≤ 1001 ≤ string length ≤ 100
Input example:
test case
Output example:
t e s t c a s e
AC code
#include<iostream> using namespace std; int main() { string a; getline(cin,a); string b; for(auto c : a) { b=b+c+' '; } b.pop_back();//Delete the last character cout<<b<<endl; return 0; }
5. Replace characters
subject
Give a string of uppercase and lowercase letters.
Replace all specific characters in the string with characters # instead.
Please output the replaced string.
Input format
There are two lines to enter.
The first line contains a string no longer than 30.
The second line contains a character that represents the specific character to replace.
Output format
Output a total of one line, which is the replaced string.
Input example:
hello l
Output example:
he##o
AC code
#include<iostream> #include<cstdio> using namespace std; int main() { char str[35]; scanf("%s\n",str); char c; scanf("%c",&c); for(int i=0;str[i];i++) { if(str[i]==c) str[i]='#'; } puts(str); return 0; }
6. String insertion
subject
There are two strings strstr and substrssubstr that do not contain white space characters. The number of characters in strstr does not exceed 1010, and the number of characters in substrssubstr is 33. (the number of characters does not include \ 0 at the end of the string.)
Insert substrsubstr after the character with the largest ASCII code in strstr. if there are multiple maxima, only the first one will be considered.
Input format
The input includes several lines, each line has a set of test data, and the format is
str substr
Output format
For each set of test data, the inserted string is output.
Input example:
abcab eee 12343 555
Output example:
abceeeab 12345553
AC code
#include<iostream> using namespace std; int main() { string a,b; while(cin>>a>>b) { int p=0; for(int i=1;i<a.size();i++) { if(a[i]>a[p]) p=i; } cout<<a.substr(0,p+1)+b+a.substr(p+1)<<endl; } return 0; }
7. Characters that appear only once
subject
Give you a string that contains only lowercase letters.
Please judge whether there are characters that appear only once in the string.
If it exists, the character with the highest position in the qualified character is output.
If not, output no.
Input format
A line containing a string of lowercase letters.
The data guarantees that the length of the string does not exceed 100000.
Output format
Output the first character that meets the condition.
If not, no is output.
Input example:
abceabcd
Output example:
e
AC code
#include<cstring> #include<iostream> using namespace std; char str[100010]; int cnt[26]; int main() { int len=strlen(str); cin>>str; for(int i=0;str[i];i++) { cnt[str[i]-'a']++; } for(int i=0;str[i];i++) { if(cnt[str[i]-'a']==1) { cout<<str[i]<<endl; return 0; } } puts("no"); return 0; }