m0_64028917 2021-11-14 22:04 采纳率: 0%
浏览 90

设计一个C语言程序:由键盘输入一串字符,直到键入回车键时退出;对字母进行大小写转换;若输入的不是字母则报错;并统计出键入的大写字母数、小写字母数和非字母数。

设计一个C语言程序:由键盘输入一串字符,直到键入回车键时退出;对字母进行大小写转换;若输入的不是字母则报错;并统计出键入的大写字母数、小写字母数和非字母数。

  • 写回答

1条回答 默认 最新

  • boonion 2021-11-14 22:58
    关注
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc,char *argv[])
    {
        char *str = (char *)malloc(sizeof(char)*100);
        int ch_max = 0;
        int ch_min = 0;
        int ch_other = 0;
        int i = 0;
    
        printf("please entern the str: ");
        scanf("%[^\n]",str);//带空格输入,scanf("%s",str);不带空格输入
    
        printf("input str = %s\n",str);
    
        while(str[i] != '\0')
        {
            if( str[i] >= 'a' && str[i] <= 'z')//判断字符为小写
            {
                ch_min ++;//小写统计加一
                str[i] = 'A' + (str[i] - 'a');//转换为大写        
            }
            else if ( str[i] >= 'A' && str[i] <= 'Z')
            {
                ch_max ++;
                str[i] = 'a' + (str[i] - 'A');            
            }
            else
                ch_other++;
            i++;
        
        }
    
        if(ch_other == 0)//如果没有其他字符,正常输出,否则错误输出
            printf("output str = %s\n",str);
        else
            printf("input error, ch_min = %d , ch_max = %d ,ch_other = %d\n",ch_min,ch_max,ch_other); 
    
        return 0 ;
    }
    

    img

    img

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 11月14日