0

I have this practice exercise here. When I type in a sentence with a number of spaces, I expect it to tell me that number. However, it will only say 0 spaces. Am I making one small stupid mistake?

#include <stdio.h>
#include <ctype.h>

#define COUNT 40

int main()
{
  char input[COUNT];
  printf("Enter a sentence: ");
  scanf("%s", input);

  int loopCount;
  int spaceCount = 0;

  for(loopCount = 0; loopCount < COUNT; loopCount++)
  {
    if (isspace(input[loopCount]))
    {
      spaceCount++;
      printf("SO MUCH STUFF");
    }
    else{}
  }

  printf("That sentence has %d spaces.", spaceCount);
  return(0);
}
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71
Tim Ford
  • 5
  • 2
  • 4
    scanf will only give you the first word.. – amdixon Jul 19 '15 at 04:35
  • possible duplicate of [How do you allow spaces to be entered using scanf?](http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf) – n. m. could be an AI Jul 19 '15 at 05:19
  • By the way, you should change your loop condition a little, maybe `for(loopCount = 0; input[loopCount] && loopCount < COUNT; loopCount++)`, so that the loop won't continue past the end of the string you read in if it was shorter than your buffer. – Dmitri Jul 19 '15 at 06:03
  • regarding this line: ''scanf("%s", input); 1) scanf will stop at the first white space ( typically space, tab, newline) character that is encountered. 2) always check the returned value from scanf (not the parameter values) to assure the operation was successful. 3) put a max length modifier on the %s, otherwise the user can overrun the input buffer, leading to undefined behaviour and can/will lead to a seg fault event. – user3629249 Jul 19 '15 at 23:31
  • when #define'ing a numeric value, always wrap the value in parens '(', ')' to avoid any 'text-replacement' errors – user3629249 Jul 19 '15 at 23:33
  • why have the else clause when it does nothing? regarding this line: 'for(loopCount = 0; loopCount < COUNT; loopCount++)' do not use this line as it will look at characters past the end of the actual input. Rather use something like: for(loopCount=0; input[loopCount]; loopCount++) which will exit the loop when the trailing NUL byte of the input string is encountered – user3629249 Jul 19 '15 at 23:38

2 Answers2

4

scanf with %s modifier will read one string ( using whitespace to mark end )..

to fix, use fgets

code

#include <stdio.h>
#include <ctype.h>

#define COUNT 40

int main()
{
  char input[COUNT];
  printf("Enter a sentence: ");

  // read line of string ( or first COUNT characters )
  // this is also a safer alternative for buffer overflow
  fgets(input, COUNT, stdin);

  int loopCount;
  int spaceCount = 0;

  for(loopCount = 0; loopCount < COUNT; loopCount++)
  {
    if (isspace(input[loopCount]))
    {   
      spaceCount++;
    }
  }

  printf("That sentence has %d spaces.\n", spaceCount);
  return 0;
}

output

$ ./test
Enter a sentence: sentence with spaces
input: sentence with spaces

That sentence has 3 spaces.
amdixon
  • 3,814
  • 8
  • 25
  • 34
1

@amdixon is quite right, scanf only reads up to whitespace, so the way you usually do stuff like this with scanf is with a while loop, but both fgets and reading into a statically allocated array are quite iffy things. The beautiful thing here is that you can lazy to great effect. Something like this would work rather well:

char next = 0;
while (next != '\n')
{
    scanf("%c", &next);
    if (next == ' ')
        spaceCount++;
}
joshbooks
  • 489
  • 3
  • 8