what would I use to count number of words

This is a discussion on what would I use to count number of words within the C Programming forums, part of the General Programming Boards category; << split from long dead thread http://cboard.cprogramming.com/c-pro...ber-words.html >> Code: #include<stdio.h> #include<conio.h> #include<string.h> main() { clrscr(); char *str,a[]=".!?"; int index,cnt,sntcnt=0,slen=0; printf("Enter ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:39 PM
  5. counting the number of words
    By ccoder01 in forum C Programming
    Replies: 3
    Last Post: 05-29-2004, 02:38 AM

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