第10次作业

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

#include<stdio.h>

void main()

{

int num=0,i;

char chr[100];

printf("请输入字符串");

scanf("%s",chr);

for(i=0;i<100;i++){

          if(chr=='\r'||chr=='\n'&&chr==0){

                  break;

          }

          num++;

}

printf("length=%d\r\n",num);

}

 

 

 

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

#include<stdio.h>

 main()

{

char str[20];

int i,cnt;

cnt=i=0;

gets(str);

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

{

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

                 cnt++;

        i++;

}

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

 }

 

 

 

3.编写程序,去掉字符串中所有的星号。例如,‘‘***AB**C***DE**FG***’’,处理完为‘‘ABCDEFG’’。

#include<stdio.h>

 main()

{

char str[20];

int i,j;

i=j=0;

gets(str);

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

{

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

        str[j++]=str[i];

        i++;

}

i=0;

while(i<j)

 putchar(str[i++]);

}

 

 

 

4.编写程序,将字符数组a中的字母复制到字符数组b中,要求每三个字符后插入一个空格。例如,字符数组‘‘ABCDEFGHIJK’’,字符数组b:‘‘ABC DEF GHI JK’’。

#include<stdio.h>

 main()

{

char a[20],b[20];

int i,j;

gets(a);

for(i=j=0;a[i]!='\0';i++)

{

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

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

b[j++]=' ';

}

b[i]='\0';

puts(b);

}

 

 

 

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

#include<stdio.h>

 main()

{

char str[80];

int i=0;

gets(str);

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

{

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

putchar(str[i]);

i++;

}

 }

 

 

 

6.统计字符串中各数字字符串的个数。

#include<stdio.h>

 main()

{

char str[80];

int cnt[10]={0};

int i=0;

gets(str);

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

{

        if(str[i]>='0'&&str[i]<='9')

cnt[(str[i]-'0')%10]++;

i++;

 }

 for(i=0;i<=9;i++)

         printf("数学字符%d:%d个\n",i,cnt[i]);

 }

 

 

posted @ 2021-11-23 20:05  杨阿浩  阅读(75)  评论(0)    收藏  举报