【问题标题】:Checking if all elements in array are zero检查数组中的所有元素是否为零
【发布时间】:2016-04-07 02:31:05
【问题描述】:

我无法确定输入的两个单词是否是字谜。

#include <stdio.h>
#include <string.h>

int main() {
    char ch;
    int letter_count[26] = {0};
    int i;
    int sum = 0;

    printf("Enter first word: ");
    do
    {
       scanf("%c", &ch);
       letter_count[ch - 'a']++;
    } while (ch != '\n');
    for(i = 0; i < 26; i++)
      printf("%d ", letter_count[i]);
    printf("\n");

    printf("Enter second word: ");
    do
    {
       scanf("%c", &ch);
       letter_count[ch - 'a']--;
    } while (ch != '\n');
    for(i = 0; i < 26; i++)
        printf("%d ", letter_count[i]);
    for(i = 0; i < 26; i++)
        if(letter_count[ch] != 0)
            sum++;

    if (sum == 0)
        printf("anagrams");
    else
        printf("not anagrams");
}

我必须使用代码的 do while 部分。我可以输入这两个词,它会打印出数组中的元素,这样“Mattress”和“Smartest”就会使所有元素都为零。但是,我在最后一部分遇到了麻烦,即使用第三个循环来检查所有元素是否为零。

我想我可以事先声明一个 int 并在元素不为零时让它递增,并且我可以让任何大于零的总和都不是字谜。但是,它总是为我打印出字谜。

【问题讨论】:

  • 由于越界访问导致的未定义行为。
  • sum++ 替换为bad = true。并确保将大写字母转换为小写。
  • @user3386109:鉴于letter_count[ch - 'a'] 是在 循环检查ch != ' \n' 之前访问的,将ch 转换为小写并没有多大帮助。
  • 请注意,在for(i=0; i &lt; 26; i++) if(letter_count[ch] != 0) sum++; 中,您的循环递增i,但您使用ch 而不是i 检查。这是您问题的主要部分。不要忘记在第二批字母计数后输出换行符。并且您需要在使用该值之前检查是否读取了一个字母 - 如果 ch 包含换行符,那么当您使用它来设置由负数索引的 letter_count 时会出现问题,等等。修改您的输入以使用getchar().
  • @EOF 好的,多亏了乔纳森的评论,我明白了你的意思,但 OP 仍然必须将大写字母转换为小写字母。

标签: c


【解决方案1】:

在您的第三个循环中,使用 letter_count[ch] 不会检查整个数组。您应该使用循环变量i 遍历数组。那部分代码应该是:

for (i=0; i<26; i++)
    if (letter_count[i] != 0)
        sum++;

【讨论】:

    【解决方案2】:

    要同时处理大写和小写字母,请在 &lt;ctype.h&gt; 中使用 topper()to lower() 以避免越界访问。

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h> // <---
    
    int main() {
        char ch;
        int letter_count[26] = {0};
        int i;
        _Bool bad = 0;
    
        printf("Enter first word: ");
        do
        {
            scanf("%c", &ch);
            if(!isalpha(ch)) // <---
            {
                puts("Not a letter");
                continue;
            }
            letter_count[tolower(ch) - 'a']++; // <---
        } while (ch != '\n');
        for(i = 0; i < 26; i++)
            printf("%d ", letter_count[i]);
        printf("\n");
    
        printf("Enter second word: ");
        do
        {
            scanf("%c", &ch);
            if(!isalpha(ch)) // <---
            {
                puts("Not a letter");
                continue;
            }
            letter_count[tolower(ch) - 'a']--; // <---
        } while (ch != '\n');
        for(i = 0; i < 26; i++)
            printf("%d ", letter_count[i]);
        printf("\n"); // <---
    
        for(i = 0; i < 26; i++)
            if(letter_count[i] != 0)
            {
                bad = 1;
                break; // <---
            }
        if (bad == 0)
            printf("anagrams");
        else
            printf("not anagrams");
    }
    

    查看所有标记为// &lt;---的地方。

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2018-05-13
      • 2021-06-20
      相关资源
      最近更新 更多