【问题标题】:number of chars in a string with scanf() and switch-statment带有 scanf() 和 switch 语句的字符串中的字符数
【发布时间】:2016-02-17 13:58:01
【问题描述】:

我想做什么

我正在尝试计算字符串中的字符数。我尝试将字符串保存在数组CH[100] 中,然后启动switch-statement 以查找字符Q 重复了多少...

守则

114.c

#include <stdio.h>
#define SIZE 100
int main(int argc, char* argv[])
{
    char* CH[SIZE];
    int alpha[26] = { 0 };
    unsigned int i = 0; 

    printf("name\n");
        scanf("%s", CH);
    while(i < SIZE){
        if(CH[i] != '\0')
        {   
            switch(CH[i])
            {
                case 'a':
                case 'A': ++alpha[0]; break;
                case 'b':
                case 'B': ++alpha[1]; break;
                case 'c':
                case 'C': ++alpha[1]; break;
                .           .           .   
                .           .           .
                .           .           .
                case 'y': 
                case 'Y': ++alpha[24]; break;
                case 'z': 
                case 'Z': ++alpha[25]; break;
            }//end switch
        }else{ break;}
    ++i;
    }//end while
    for(int j = 65; j < 91; ++j)
    {
        if(alpha[j- 65] != 0)
        printf("%s\t - %d times\n",(char) j,alpha[j- 65]);
    }
}

编译和执行:

[ar.lnx@host Documents] $ gcc 114.c -o x
[ar.lnx@host Documents] $ ./x
donner le nom
anas
Segmentation fault (core dumped)
[ar.lnx@host Documents] $

我不明白是什么问题,谁能帮我看看这里发生了什么

【问题讨论】:

  • 检查索引!并使用更好的测试字符串,例如abc...z。顺便说一句:那太容易出错了;使用代码直接索引到数组中。并且不要对变量使用全大写。
  • 你使用%s来打印一个字符;请改用%c。 (%s 需要 char *,它会取消引用。作为可变参数传递的 ASCII 值不是有效地址,可能没有正确的大小。编译时启用警告以了解格式/参数不匹配。)
  • 注意,您可以通过设置 len=strlen(CH); 并将 while 设为 for 循环 for(i=0; i&lt;len; i++) 来摆脱 while 内的 if

标签: c arrays string switch-statement scanf


【解决方案1】:

好吧,将%s 中的%c 更改为printf。

做

printf("%c\t - %d times\n",(char) j,alpha[j- 65]);

而不是

printf("%s\t - %d times\n",(char) j,alpha[j- 65]);

您更正的代码是

#include <stdio.h>
#define SIZE 100
int main(int argc, char* argv[])
{
    char CH[SIZE];
    int alpha[26] = { 0 };
    unsigned int i = 0; 

    printf("name\n");
        scanf("%s", CH);
    while(i < SIZE){
        if(CH[i] != '\0')
        {   
            switch(CH[i])
            {
                case 'a':
                case 'A': ++alpha[0]; break;
                case 'b':
                case 'B': ++alpha[1]; break;
                case 'c':
                case 'C': ++alpha[1]; break;
                case 'y': 
                case 'Y': ++alpha[24]; break;
                case 'z': 
                case 'Z': ++alpha[25]; break;
            }//end switch
        }else{ break;}
    ++i;
    }//end while
    int j;
    for( j = 65; j < 91; ++j)
    {
        if(alpha[j- 65] != 0)
        printf("%c\t - %d times\n",(char) j,alpha[j- 65]);
    }
}

如果您对%c 和%s 之间的区别有疑问,请查看this。

【讨论】:

  • 您可能应该突出显示此修改:您的代码中的 char CH[SIZE]; 与 OPs 代码中的 char * CH[SIZE];。这应该是分段错误的原因。
【解决方案2】:

问题在于您的printf 格式:

printf("%s\t - %d times\n",(char) j,alpha[j- 65]);

改成这样:

printf("%c\t - %d times\n",j,alpha[j- 65]);

char 演员表是无害的,但没有必要。真正的问题是%s 必须是%c。

【讨论】:

    【解决方案3】:

    唯一的错误是在打印计数时将 %s 替换为 %c 即改变

    printf("%s\t - %d times\n",(char) j,alpha[j- 65]);
    

    到

    printf("%c\t - %d times\n",(char) j,alpha[j- 65]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2019-08-26
      • 2012-10-24
      • 1970-01-01
      • 2013-04-06
      相关资源
      最近更新 更多