Code not working as expected in C

2019-07-22 12:40发布

I was working on a program in C to count the number of spaces in a sentence. But I haven't managed to get it to work properly. If I enter something like Hello world 1234 how are you the output I'm getting is 3 when the output expected is 5.
My code is :

//Program to count number of words in a given Sentence
#include <stdio.h>
#include <string.h>
int main()
{
    char sent[100];
    char sentence[] = {' ', '\0'};
    printf("\nEnter a sentence :\n");
    gets(sent);
    strcat(sentence, sent);
    int l = strlen(sentence), i = 0, count = 0, countCh = 0;
    printf("%d", l);
    char ch, ch1;
    for (i = 0; i < (l-1); i++)
    {
        ch = sentence[i];
        if (ch == ' ')
        {
            ch1 = sentence[i+1];
            if (((ch1 >= 'A') && (ch1 <= 'Z'))||((ch1 >= 'a') && (ch1 <= 'z')))
                count++;
        }
    }
    printf("\nNo of words is : %d", count);
    return 0;
}

I used the same logic in Java and it worked fine. Could someone explain whats going wrong?

2条回答
疯言疯语
2楼-- · 2019-07-22 12:43

The following

       if (((ch1 >= 'A') && (ch1 <= 'Z'))||((ch1 >= 'a') && (ch1 <= 'z')))
            count++;

probably should be

       if (ch1 != ' ')
            count++;

As now " 12345" would not be counted as word.

Also count counts the spaces, so the word count is one more: hence 3 instead of 5.

Your sentence seems to have had the intention of counting the terminatin NUL.

If you want to count real words containing letters, use a bool state whether current and prior state of being in a letter differ.

As mentioned overflow is possible with your code.

查看更多
SAY GOODBYE
3楼-- · 2019-07-22 13:01

The problem in your code is with the definition of sentence. When you leave out the array dimension and initialize it, the size of the array will be determined by the length of the initializer.

Quoting the man page of strcat()

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable;

That is, the program will invoke undefined behavior.

This way, sentence has certainly way less memory than it it supposed to hold. Moreover, strcat() is not at all required there.

The correct way to do it will be

  • Define sentence with a proper dimention, like char sentence[MAXSIZE] = {0};, where MAXSIZE will be a MACRO having the size of your choice.
  • use fgets() to read the user input.
  • use isspace() (from ctype.h) in a loop to check for presence of space in the input string.
查看更多
登录 后发表回答