插入字符串
题目内容:
从键盘输入一个字符串,并在串中的第一次出现的最大元素后边插入字符串”ab”。
输入格式:
任意输入一个字符串
输出格式:
在串中的最大元素后边插入字符串”ab”
输入样例:
123csCUMT
输出样例:
123csabCUMT
#include <iostream>
using namespace std;
#include <string>
int main(){
bool t = true;
string s;
cin>>s;
char max;
max = s[0];
int n = s.length(),tag;
for(int i = n-1;i>=0;i--){
if(s[i]>=max){
max = s[i];
tag = i;
t = false;
}
}
if(!t){
for(int i = 0;i<=tag;i++){
cout<<s[i];
}
cout<<"ab";
for(int i = tag+1;i<n;i++){
cout<<s[i];
}
}
else{
cout<<s[0]<<"ab";
for(int i = 1;i<n;i++){
cout<<s[i];
}
}
return 0;
}
统计整数个数
题目内容:
输入一个字符串,其包括数字和非数字字符,如:a123x456 17935? 098tab,将其中连续的数字作为一个整数,依次存放到数组a中,统计共有多少个整数,并输出这些数。
输入格式:
数字和非数字字符的字符串
输出格式:
1)整数个数2)分别输出整数
输入样例:
a123x456 17935? 098tab583
【注意需要保留带有空格的字符串,请不要使用gets,cin,练习使用cin.getline(char *str, int maxnum)】
输出样例:
5
123
456
17935
98
583
#include <iostream>
using namespace std;
#include <string>
int main(){
int count =0;
int t = 0;
char s[100];
char p[100];
cin.getline(s,100);
int i = 0;
while(s[i]!='\0'){
if(s[i]>='0'&&s[i]<='9'){
p[t++] = s[i];
}
else{
p[t++] = '_';
}
i++;
}
p[t] = '_';
p[t+1] = '\0';
int j = 0;
while(p[j]!='\0'){
if(p[j]>='0'&&p[j]<='9'&&p[j+1] == '_'){
count++;
}
j++;
}
j = 0;
cout<<count<<endl;
while(p[j]!='\0'){
if(p[j]>'0'&&p[j]<='9')
cout<<p[j];
if(p[j]>='0'&&p[j]<='9'&&p[j+1] == '_')
cout<<endl;
j++;
}
return 0;
}
字符串排序
题目内容:
有5个字符串,首先将它们按照字符串中字符个数由小到大排序,再分别取出每个字符串的第三个字母合并成一个新的字符串输出(若少于三个字符的输出空格)。要求:利用字符串指针和指针数组实现。
输入格式:
5个字符串,用回车分隔
输出格式:
输出一个字符串:按5个字符串中字符个数由小到大排序,每个字符串后面有一个空格;再分别取出每个字符串的第三个字母合并成一个新的字符串输出,若少于三个字符的输出空格
输入样例:
test1234
123test
cumt
think
apples
输出样例:
cumt think apples 123test test1234
concatenate string:mip3s
#include <iostream>
using namespace std;
#include <string>
int main(){
string s[5];
int length[5];
for(int i = 0;i<5;i++){
cin>>s[i];
}
for(int i = 0;i<4;i++){
for(int j = 0;j<4-i;j++){
if(s[j].length()>s[j+1].length()){
string t = s[j];
s[j] = s[j+1];
s[j+1] = t;
}
}
}
for(int i = 0;i<5;i++){
cout<<s[i]<<" ";
}
cout<<endl;
cout<<"concatenate string:";
for(int i = 0;i<5;i++){
if(s[i][2]!='\0'){
cout<<s[i][2];
}
else{
cout<<" ";
}
}
return 0;
}