用数组计算时的无限循环[C]

时间:2015-10-25 11:01:28

标签: c

我最近一直试图回到C(出于工作目的)并且我去过C进修研讨会。我似乎无法理解为什么在此代码中出现无限循环。

我试图编写一个程序,该程序返回一个字符数组中的单词数。任何帮助将不胜感激。谢谢!

// ArrayWords.c
#include <stdio.h>
#include <ctype.h>
#define LENGTH 50

int word_count(char []);

int main(void) {

    char sentence[LENGTH];
    int i;

    printf("Enter a sentence with at most %d characters:\n", LENGTH);

    for(i = 0; i < LENGTH; i++)
        scanf("%s", &sentence[i]);

    printf("Sentence = %s\n", sentence); 

    printf("Word count = %d\n", word_count(sentence));

    return 0;
}

// Count the number of words in str
int word_count(char str[]) {

    int i, word = 1;

    for(i = 0; i < LENGTH; i++)
        while(str[i] != '\0')
            if((isspace(str[i])))
            {   
                word++;
            }

    return word;
}

6 个答案:

答案 0 :(得分:1)

  • 您对scanf的使用并不好。
  • word_count导致无限循环,因为i循环中未更新while

固定代码:

// ArrayWords.c
#include <stdio.h>
#include <ctype.h>
#define LENGTH 50

int word_count(char []);

int main(void) {

    char sentence[LENGTH];

    printf("Enter a sentence with at most %d characters:\n", LENGTH);

    fgets(sentence, sizeof(sentence), stdin);

    printf("Sentence = %s\n", sentence); 

    printf("Word count = %d\n", word_count(sentence));

    return 0;
}

// Count the number of words in str
int word_count(char str[]) {

    int i = 0, word = 1;

    while(str[i] != '\0') {
        if((isspace(str[i])))
        {   
            word++;
        }
        i++;
    }

    return word;
}

答案 1 :(得分:0)

for(i = 0; i < LENGTH; i++)
    scanf("%s", &sentence[i]);

在这里,您逐个阅读char。所以它应该%c,例如scanf(" %c", &sentence[i]);

但是,这不是读取字符串的正确方法。因为,它不会将\0放在最后。

由于要读取的字符串将包含空格,因此最好的方法是fgets()

您的计数循环也有错误,要么您可以将其更改为上述答案之一,要么只使用一个for循环,就像这样

for(i = 0; i < LENGTH && str[i] != '\0'; i++)
    if((isspace(str[i])))
    {   
        word++;
    }

答案 2 :(得分:0)

该计划的这一部分

for(i = 0; i < LENGTH; i++)
    scanf("%s", &sentence[i]);

没有意义。它尝试在循环中输入LENGTH个单词。

我认为您的意思是格式说明符%c而不是%s

    scanf("%c", &sentence[i]);

但即使在这种情况下,这段代码也是错误的,因为用户可以输入少于LENGTH个字符或甚至大于LENGTH个字符,而字符数组sentence将不会零结尾..

在这种情况下,最好使用标准函数fgets而不是scanf

功能word_count也是错误的。例如,如果str将被零终止,那么外部for循环将尝试计算字符串之外的单词。实际上,函数计算空间的数量。它与单词数不同。字符串可以包含相邻的空格。所有其他人在他们的答案中都犯了这个错误。:) 在while循环变量中,我没有增加。所以这是一个无限循环。

int word_count(char str[]) {

    int i, word = 1;

    for(i = 0; i < LENGTH; i++)
        while(str[i] != '\0')
            if((isspace(str[i])))
            {   
                word++;
            }

    return word;
}

程序可以按以下方式查看

// ArrayWords.c
#include <stdio.h>
#include <ctype.h>

#define LENGTH 50

size_t word_count( const char[] );

int main( void )
{
    char sentence[LENGTH];

    printf( "Enter a sentence with at most %d characters: ", LENGTH );
    fgets( sentence, LENGTH, stdin );

    printf( "Sentence = \"%s\"\n", sentence ); 

    printf( "Word count = %zu\n", word_count( sentence ) );

    return 0;
}

// Count the number of words in str
size_t word_count( const char s[] ) 
{
    size_t words = 0;

    while ( *s != '\0' )
    {
        //  skip white spaces
        while ( isspace( ( unsigned char )*s ) ) ++s;

        if ( *s != '\0' ) ++words;

        //  skip the word
        while ( *s != '\0' && !isspace( ( unsigned char )*s ) ) ++s;
    }

    return words;
}

它的输出可能看起来像

Enter a sentence with at most 50 characters: Hello, Unholy Wish
Sentence = "Hello, Unholy Wish"
Word count = 3

答案 3 :(得分:0)

在你的代码中,i实际上并不在循环中。除此之外,数组最后包含一个\0的句子,所以如果句子的长度为50,则空格必须是51,因为在它的最后有一个\0,而scanf不是很好,因为它将" "判断为停止标志,gets不能停止输入可能导致溢出的密钥,因此我使用fgets

#include <stdio.h>
#include <ctype.h>
#define LENGTH 50
int word_count(char []);
int main(void) {
    char sentence[LENGTH+1];
    printf("Enter a sentence with at most %d characters:\n", LENGTH);
    fgets(sentence, sizeof(sentence), stdin);
    printf("Sentence = %s\n", sentence);
    printf("Word count = %d\n", word_count(sentence));
    return 0;
}

// Count the number of words in str
int word_count(char str[]) {
    char* p=str;
    int word=0;
    while(*p!='\0'){
        if((isspace(*p))) word++;
        p++;
    }
    return word;
}

答案 4 :(得分:0)

你应该从stdin读取数据,而不是scanf但fgets或获取! 也许fgets(句子,LENGTH,stdin)很好。 在函数word_count中,您可以使用if替换。

答案 5 :(得分:-1)

它不是一个无限循环。

您的程序正在尝试阅读50个(或LENGTH)个单词(并将它们保存在彼此之上)。

    for(i = 0; i < LENGTH; i++)
        scanf("%s", &sentence[i]);

首先从sentence[0]开始; 从sentence[1]开始的第二个字覆盖第一个字的字符; ...