Thread: Word counting

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

    Word counting

    <<mod note: split from 2 year old thread>>
    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;
    }
    yea these are the codes i have..it works kinda ok but it just counts as the condition applies...im having trouble trying to figure out the problem with this...can provide some help pls..im really new to this...

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    From the forum rules.
    Please follow the forum structure when posting. Post threads on the board best suited for the topic. Please do not cross post (i.e. post the same question on multiple boards). Do not bump threads. (Bumping: Posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You should have read the other thread, not bumped it.

    char *str;
    gets(str);


    The absolute worst two lines of C you'll ever see.
    str is uninitialised - you've no idea where your input is going to go
    gets() is unconstrained - you've no idea how much input is going to an unknown place.

    Read the FAQ, and ALWAYS use fgets()
    Eg
    char buff[100];
    fgets( buff, sizeof(buff), stdin );
    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. String - word counting!
    By shardin in forum C Programming
    Replies: 2
    Last Post: 07-11-2007, 05:08 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. Word Counting
    By Achillles in forum C++ Programming
    Replies: 9
    Last Post: 09-11-2002, 02:09 PM
  5. follow up with char I/O and line, word, and char counting
    By Led Zeppelin in forum C Programming
    Replies: 1
    Last Post: 03-23-2002, 09:26 PM