1编写程序,使用循环结构,计算字符串长度。#include<stdio.h>main(){ char a[50]; int i; gets(a); for(i=0;a[i]!='\0';i++); printf("%d",i);} |

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
2编写程序,统计大写字母。#include<stdio.h>#include<string.h>main(){ char a[50]; int i,j,flag=0; gets(a); i=strlen(a); for(j=0;j<i;j++){ if(a[j]<=90&&a[j]>=65){ flag++; } } printf("%d",flag);} |

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
3编写程序,去掉字符串中所有的星号。#include<stdio.h>#include<string.h>main(){ char a[50]="****AB**C***DE**FG***"; char b[50]="0"; int i,j,flag=0; i=strlen(a); for(j=0;j<i;j++){ if(a[j]=='*'){ }else{ b[flag++]=a[j]; } } puts(b);} |

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
4编写程序,将字符数组a中的字母复制到字符数组b中,要求每三个字符后插入一个空格。#include<stdio.h>#include<string.h>main(){char a[50]="ABCDEFGHIJK";char b[50];int i,j,x=0,y=0;i=strlen(a);for(j=0;j<i;j++){ b[j]=a[x]; x++; y++; if(y%3==0){ j=j+1; b[j]=' '; i++; }}b[j]='\0';puts(b);} |

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
5输出字符串中位置为奇数,ASCII码为偶数的字符。#include<stdio.h>#include<string.h>main(){ char a[50]; int i,sum; gets(a); sum=strlen(a); for(i=0;i<sum;i++){ if((i+1)%2!=0&&a[i]%2==0){ printf("%c",a[i]); } }} |

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
6统计字符串中各数字的个数#include<stdio.h>#include<string.h>main(){ char a[50]; int i,j=0,sum,x; int sum0=0,sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0,sum7=0,sum8=0,sum9=0; gets(a); sum=strlen(a); for(i=0;i<sum;i++){ if(a[i]>=48&&a[i]<=57){ x=a[i]-48; switch(x){ case 1:sum1++;break; case 2:sum2++;break; case 3:sum3++;break; case 4:sum4++;break; case 5:sum5++;break; case 6:sum6++;break; case 7:sum7++;break; case 8:sum8++;break; case 9:sum9++;break; case 0:sum0++;break; } } } printf("0有%d\n1有%d\n2有%d\n3有%d\n4有%d\n5有%d\n6有%d\n7有%d\n8有%d\n9有%d\n",sum0,sum1,sum2,sum3,sum4,sum5,sum6,sum7,sum8,sum9);} |

浙公网安备 33010602011771号