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