Thread: what would I use to count number of words

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    what would I use to count number of words

    << split from long dead thread what would I use to count number of words >>
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
    clrscr();
    char *str,a[]=".!?";
    int index,cnt,sntcnt=0,slen=0;
    printf("Enter string: ");
    gets(str);
    slen=strlen(str);
    for (index=0;index<slen;index++)
        {
        for (cnt=0;cnt<4;cnt++)
            {
            if (str[index]==a[cnt]||str[index]=='\0')
                {
                sntcnt;
                }
            }
        }
    printf("Number of sentence in the string: %d",sntcnt);
    getch();
    return 0;
    }
    
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
    clrscr();
    char *str;
    int index,slen=0,wrdcnt=0;
    printf("Enter string: ");
    gets(str);
    slen=strlen(str);
    for (index=0;index<=slen;index++)
        {
        if (str[index]==' '||str[index]==','||str[index]=='.'||str[index]=='!'||str[index]=='?'||str[index]=='\0')
            {
            wrdcnt++;
            if (str[index]==' ')
                {
                wrdcnt--;
                }
            else
                {
                wrdcnt++;
                }
            }
        }
    printf("The number of words in the string is: %d",wrdcnt);
    getch();
    return 0;
    }
    help with my codes guys..im really new >.<

    if i input more than 2 spaces between the words the program also counts em >.<

    same with the sentence count program

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Since you seem to be suffering from a lot more than lack of C knowledge...

    Read your replies here, rather than bumping the same dead thread with the same code.
    Word counting
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. Please Explain Count the number of bits in an int code
    By dnysveen in forum C++ Programming
    Replies: 36
    Last Post: 12-23-2006, 10:39 PM
  5. counting the number of words
    By ccoder01 in forum C Programming
    Replies: 3
    Last Post: 05-29-2004, 02:38 AM