codeup 字符串处理
题号:10000580
A题 :字符串连接
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=0
#include <cstdio>
int main(){
char str1[210];
char str2[110];
while(scanf("%s%s",&str1,&str2)!=EOF){
int i;
for(i=0;str1[i]!='\0';i++);
for(int j=0;str2[j]!='\0';j++){
str1[i++]=str2[j];
}
str1[i]='\0';
printf("%s\n",str1);//用printf要加"\n"
// puts(str1);//用这个输出完全正确
}
return 0;
}
运行结果:
B题 :首字母大写
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=1
#include <cstdio>
#include <cstring>
int main(){
char str[128];
while(gets(str)){
int len = strlen(str), r=0,h=0,i;
char ans[100][100]={};
for(i=0;i<len;i++){
if(str[i] != ' ' && str[i] !='\t' && str[i] != '\r' && str[i] !='\n' )
ans[r][h++] = str[i];
else{
ans[r++][h] = '\0';
h=0;//h重新置0
}
}
for(i=0;i<=r;i++){
if(ans[i][0]>='a' && ans[i][0]<='z')
ans[i][0] -=32;
}
//输出二维数组
for(i=0; i<=r; i++){
printf("%s",ans[i]);
if(i<r)
printf(" ");
else
printf("\n");
}
}
return 0;
}
运行结果:
C题 :字符串的查找删除
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=2
#include<stdio.h>
#include<string.h>
int main()
{
int k=0;
char a[10000],ans[10000];
char temp[10000];
gets(temp);
for(int i=0; i<strlen(temp);i++)
{//A 65 a 97
if(temp[i]>='A'&&temp[i]<='Z')
temp[i]+=32;
}
int len = strlen(temp);
while(scanf("%c",&a[k])!=EOF)
{
ans[k] = a[k];
if(a[k]>='A'&&a[k]<='Z')
a[k]+=32;
if(a[k]==temp[len-1])
{
int i=0,t=0;
for(i=k-len+1; i<=k; i++)
{
if(temp[t++]!=a[i])
break;
}
if(i==k+1)
k = k-len+1;
else
k++;
}
else
k++;
}
for(int i=0; i<k; i++)
{
if(ans[i]!=' ')
printf("%c",ans[i]);
}
printf("\n");
}
运行结果:
D题 :单词替换
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=3
#include <cstdio>
#include <cstring>
int main(){
char str[1001];
char s[1001],n[1001];
while(gets(str)){
int len = strlen(str), r=0,h=0,i;
char ans[1001][1001]={};
for(i=0;i<len;i++){
if(str[i] != ' ')
ans[r][h++] = str[i];
else{
ans[r++][h] = '\0';
h=0;//h重新置0
}
}
gets(s);
gets(n);
for(i=0; i<=r; i++){
if(strcmp(ans[i],s)==0){
strcpy(ans[i],n);
}
printf("%s",ans[i]);
if(i<r-1)
printf(" ");
else
printf("\n");
}
}
return 0;
}
运行结果:
E题 :字符串去特定字符
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=4
#include <cstdio>
#include <cstring>
int main(){
char str[100];
char s;
char ans[100];
while(gets(str)){
s=getchar();
int j=0;
for(int i=0;i<strlen(str);i++){
if(str[i]!=s){
ans[j++]=str[i];
}
}
ans[j]='\0';
puts(ans);
getchar();
}
return 0;
}
运行结果:
F题 :数组逆置
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=5
#include <cstdio>
#include <cstring>
int main(){
char str[200];
while(gets(str)){
int len=strlen(str);
for(int i=len-1;i>=0;i--){
putchar(str[i]);
}
printf("\n");
}
return 0;
}
运行结果:
G题 :比较字符串
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=6
#include <cstdio>
#include <cstring>
int main(){
int count;
scanf("%d",&count);
char str1[50],str2[50];
while(count--){
scanf("%s %s",str1,str2);
int str1Len=strlen(str1);
int str2Len=strlen(str2);
if(str1Len>str2Len){
printf("%s is longer than %s\n",str1,str2);
}else if(str1Len==str2Len){
printf("%s is equal long to %s\n",str1,str2);
}else{
printf("%s is shorter than %s\n",str1,str2);
}
}
return 0;
}
//计算字符串的长度
//int arrlen(char arr[]){
// int len=strlen(arr);
// int r=0,h=0;
// char ans[50][50];
// for(int i=0;i<len;i++){
// if(arr[i] != ' ')
// ans[r][h++] = arr[i];
// else{
// ans[r++][h] = '\0';
// h=0;//h重新置0
// }
// }
// return r;
//}
运行结果:
H题 :编排字符串
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=7
#include<stdio.h>
#include<string.h>
int main()
{
int n,first,index;
char str[100][100];
while(scanf("%d",&n) != EOF){
for(int i = 0;i < n;i++){
scanf("%s",str[i]);
first = 0;
if(i < 3)
index = i + 1;
else
index = 4;
for(int j = 0;j < index;j++){
if(first)
printf(" ");
first = 1;
printf("%d=%s",j+1,str[i-j]);
}
printf("\n");
}
}
return 0;
}
运行结果:
H题 :回文
题目链接:http://codeup.cn/problem.php?cid=100000580&pid=8
#include <cstdio>
#include <cstring>
const int maxn=256;
bool judge(char str[]) {
int len=strlen(str);
for(int i=0;i<len/2;i++){
if(str[i]!=str[len-1-i]){
return false;
}
}
return true;
}
int main(){
char str[maxn];
while(gets(str)){
bool flag=judge(str);
if(flag==true){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}
运行结果: