Table of contents
first question
1.1 Topic description
[Problem description] Enter a string that includes numbers and non-numeric characters, such as: a123x456 17935? 098tab, use the consecutive numbers as an integer, store them in the array a in turn, count the total number of integers, and output these numbers .
[Input form] A string of numbers and non-numeric characters
[Output form] Multi-line, the first line outputs the number of integers, and each subsequent line outputs an integer
[Sample input] a123x456 17935? 098tab583
Note that you need to keep strings with spaces, please don't use gets, cin, practice using cin.getline(char *str, int maxnum)
[Example output]
5
123
456
17935
98
583
[Example description] The first output item is the number of integers, and the following are specific integers. Note that there is no need to output prompt text, such as: "integer is", "respectively" and other words. Output the result directly. A number is also output. There are no consecutive numbers beyond the integer range in the test case. When a number starting with 0 is encountered, 0 should be rounded off.
1.2 Source code
#include<iostream> #include<string.h> using namespace std; int main() { char a[1000]; char *p=a; int num=0; cin.getline(a,1000); if(*p>='0'&&*p<='9') { num++; } for(;*p!='\0';p++) { if((*p<'0'||*p>'9')&&(*(p+1)>='0'&&*(p+1)<='9')) { num++; } } cout<<num<<endl; p=a; for(;*p!='\0';p++) { if(*p<'0'||*p>'9') { continue; } if((*(p-1)<'0'||*(p-1)>'9')&&*p=='0') { continue; } if(*p>='0'&&*p<='9') { cout<<*p; } if((*p>='0'&&*p<='9')&&(*(p+1)<'0'||*(p+1)>'9')) { cout<<endl; } } return 0; }
Question 2
2.1 Topic description
[Problem description] There are 5 strings, first sort them according to the number of characters in the string from small to large, and then take out the third letter of each string and combine it into a new string output (if less than three-character output space). Requirements: Implemented using string pointers and pointer arrays.
[Input form] 5 character strings, separated by carriage return
[Output form] Output a string: sort the number of characters in the 5 strings from small to large, and then take out the third letter of each string and combine it into a new string output, if there are less than three character output a space
[Sample input]
test1234
123test
cumt
think
apples
[Example output]
cumt think apples 123test test1234
concatenate string:mip3s
[Example description] In the first line of the output, there is a space after each string. The relative order of strings of equal number of characters is unchanged.
2.2 Source code
#include<iostream> #include<string.h> using namespace std; int main() { char *s[5]; for(int i=0;i<5;i++) { s[i]=new char[100]; cin>>s[i]; } for(int i=0;i<4;i++) { for(int j=0;j<4-i;j++) { if(strlen(s[j])>strlen(s[j+1])) { char *tmp=s[j+1]; s[j+1]=s[j]; s[j]=tmp; } } } for(int i=0;i<5;i++) { cout<<s[i]<<" "; } cout<<endl<<"concatenate string:"; for(int i=0;i<5;i++) { if(strlen(s[i])<3) { cout<<" "; } else { cout<<s[i][2]; } } return 0; }
Question 3
3.1 Topic description
[Problem description] Input a string from the keyboard, and insert the string "ab" after the first largest element in the string.
[Input form] Enter a character string arbitrarily
[Output form] Insert the string "ab" after the largest element in the string
[Sample input] 123csCUMT
[Sample output] 123csabCUMT
[Example description] In order to ensure that the input string has spaces, please use cin.getline(char*, int); this system does not support the use of gets. Insert ab only once.
3.2 Source code
#include<iostream> #include<string.h> using namespace std; int main() { char a[1000]; int k=0; char max=0; cin.getline(a , 1000); for(int i=0;i<strlen(a);i++) { if(a[i]>max) { max=a[i]; k=i; } else continue; } for(int i=0;i<=k;i++) { cout<<a[i]; } cout<<"ab"; for(int i=k+1;i<strlen(a);i++) { cout<<a[i]; } return 0; }
May you have a bright future
May your lover be married
May you be happy in this world
I just want to face the sea and spring flowers bloom