第八次作业

1.字符串长度。

#include<stdio.h>

main()

{

     char str[80];

     int i=0;

     int length=0;

     gets(str);

     puts(str);

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

          length++;

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

}

 

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("大写字母个数为i:%d\n",cnt);

}

 

3.去掉字符串中所有的*号。

#include<stdio.h>

main()

{

     char str[20];

     int i,j;

     i=j=0;

     gets(str);

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

     {

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

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

          i++;

     }

     i=0;

     while(i<j)

          putchar(str[i++]);

     }

 

4.a复制到不,要求每三个字符 插入一个空格。

#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[j]='\0';

     puts(b);

}

 

5输入字符串中位置为奇数,ASCII为偶数的字符。

#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++;

     }

}

 

5统计字符串中各数字字符的个数。

#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-12-09 04:11  所燃己  阅读(15)  评论(0编辑  收藏  举报