第十次作业

1.用循环机构求字符串长度。

#include<stdio.h>

main(){

char cs[20];

int i=0,lenght=0;

gets(cs);

puts(cs);

while(cs[i++]!='\0')

lenght++;

printf("字符串的长度为:%d\n",lenght);

}

 

2.编写程序,统计字符串中大写字母的个数。

#include<stdio.h>

main()

{

char str[20];

int i=0,cnt=0;

gets(str);

puts(str);

while(str[i++]!='\0')

{

if(str[i]>='A'&&str[i]<='Z')

cnt++;

else

cnt+=0;

}

printf("大写字母的个数为:%d\n",cnt);

}

 

3.编写程序,去掉字符串中所有的星号。

#include<stdio.h>

main(){

char cs[20];

int i=0,j=0;

gets(cs);

puts(cs);

while(cs[i++]!='\0'){

if(cs[i]!='*')

cs[j++]=cs[i];

}

i=0;

while(i<j)

putchar(cs[i++]);

}

 

4.编写程序,将字符数组a中的字母复制到字符数组b中,要求每三个字符后插入一个空格。

#include<stdio.h>

main(){

char a[20],b[20];

int i=0,j=0;

gets(a);

puts(a);

while(a[i]!='\0'){

b[j++]=a[i];

if((i+1)%3==0)

b[j++]=' ';

i++;

}

b[j]='\0';

puts(b);

}

 

5.输出字符串中位置为奇数、ASCII为偶数的字符。

#include<stdio.h>

main(){

char a[40];

int i=0,j=0;

gets(a);

puts(a);

while(a[i]!='\0'){

if((i+1)%2==1&&a[i]%2==0)

putchar(a[i]);

i++;

}

}

    

 

posted @ 2021-11-25 22:49  浅殇之城  阅读(19)  评论(0)    收藏  举报