第十次作业

1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
}

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3.编写程序,去掉字符串中所有的星号。
#include<stdio.h>
main()
{
    char str[20];
    int i=0,j=0;
    gets(str);
    while(str[i]!='\0')
    {
        if(str[i]!='*')
            str[j++]=str[i];
        i++;
    }
    i=0;
    while(i<j)
        putchar(str[i++]);
}

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
4.编写程序,将字符数组a中的字母复制到字符数组b中,要求每三个字符后插入一个空格。
#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);
}

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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++;
    }
}

  

 

posted @ 2021-11-25 12:32  粽子有点咸  阅读(13)  评论(0)    收藏  举报