Like Tree2Likes
  • 2 Post By std10093

Need help on counting word.

This is a discussion on Need help on counting word. within the C++ Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> #include <ctype.h> int main() { char str[50]; int words = 0, len=0, i; int spaces=0; ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Lightbulb Need help on counting word.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <ctype.h>
    
    int main()
    {
    char str[50];
    int words = 0, len=0, i;
    int spaces=0;
    
    printf("\n--------------------------------------------------------------------------");
    printf("\n\nPROGRAM TO COUNT THE NUMBER OF WORDS, STRING, SPACE IN A GIVEN STRING");
    printf("\n\n--------------------------------------------------------------------------");
    printf("\n\n\t ENTER A STRING...: ");
    //gets(str);
    strcpy( str, "a b c d e");
    
    
    
    while(str[len]!='\0') len++;
    
    for(i=0;i<=len;i++)
    {
            while( isspace( str[i] )) {  spaces++; i++;  }
            {continue;}
            
    }
    for(i=0;i<len;i++)
    {
        while( !isspace( str[i] )) { i++; }
            words++;
            }
    printf("\n\t NUMBER OF WORDS IN THE ABOVE SENTENCE IS...: %u", words);
    printf("\n\n-------------------------------------------------------");
    printf("\n\t NUMBER OF STRING IN THE ABOVE SENTENCE IS...: %u", len);
    printf("\n\n-------------------------------------------------------");
    printf("\n\t NUMBER OF STRING IN THE ABOVE SENTENCE IS...: %u", spaces);
    printf("\n\n-------------------------------------------------------");
    
    
    
    getch();
    return 0;
    }
    Hi, everything work fine if i input some number or char.
    example: strcpy( str, "a b c d e");
    output: 5 word 9 string 4 space
    But if i input: strcpy( str, "[space][space][space]");
    Output: 3 word 3 string 3 space

    This is the error. I should be getting 0 word 3 string 3 space.

    Can someone helping me out?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    Code:
    for(i=0; i<len; i++)
    {
        while( !isspace( str[i] ))
        {
            i++;
        }
        words++;
    }
    With this, you can see that words is counted up no matter what. I think if word switched places with i and then the i part got deleted, the code would work better.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    Moreover notice, that this is C and not C++.

    The forum has a thread for C and a thread for C++
    Elysia and Crossfire like this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a fix to word counting
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-25-2011, 05:55 AM
  2. Word counting
    By yuuh in forum C Programming
    Replies: 2
    Last Post: 08-09-2009, 11:47 PM
  3. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 03:58 AM
  4. Word Counting
    By cookie in forum C Programming
    Replies: 18
    Last Post: 06-17-2007, 12:31 PM
  5. Word Counting
    By Achillles in forum C++ Programming
    Replies: 9
    Last Post: 09-11-2002, 02:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21