Thread: prob with program to count length of words in a string

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    46

    prob with program to count length of words in a string

    I am making a program that can print out the length of each word in the string...I assumed there are 5 words in the prog(because i cant write code where the the number of words isnt constant) but the prog gives me the first 3 words with 0 chars and the other 2 with 2 big numbers
    Code:
    main(){
          char s[100];
          int j,a[5],length,i;
          printf("Enter the text : ");
          scanf("%s",s);
          length=0;
          j=0;
          i=0;
          while(s[i]!='\0'){
                          if(s[i]=' ')
                          i++;
                          else
                          while(s[i]!=' '){
                          length++;
                          i++;}
                          a[j]=length;
                          j++;
                          length=0;
                          }
          for(i=0;i<5;i++)
          printf("Word %d : %d characters\n",i+1,a[i]);
          }

  2. #2
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Code:
    if(s[i]=' ')
    Right off the bat that's an assignment in a conditional expression.
    And main returns an int

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    46
    Thanks sysop
    but still the prog doesnt output the right number of characters and also whatever words i input it says that word 2 and 4 have zero characters

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    scanf() isn't doing what you think it is -- anyway, it's more practical to use fgets() and then kill the \nl

    Code:
      char *p;
      fgets (s, 100, stdin);
      if ((p = strchr (s, '\n')) != NULL)
        *p = '\0';
    There's no reason you can't parse the line in a single loop; also, your indenting isn't very nice.

    Maybe this is better:

    Code:
      for (i = 0, j = 0; s[i] != '\0' && j < 5; i++)
      {
        if(s[i] == ' ')
        {
          a[j] = length;
          length = 0;
          j++;
          continue;
        }
        length++;
      }
      /* you need to get the last one, too */
      a[j] = length;

  5. #5
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    replace yuor scanf with

    gets(s);
    strcat(s, " ");

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Use fgets() not gets(). Sheesh... get with the program.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    I might be very wrong. Test it.

    Dont take my word. try diffrent inputs like
    > 1
    > enter
    > 1234 12
    > 1234 12 12
    > abc 123

    Are they all working ????

    Code:
    int  main(void)
    {
    char   s[100];
    int    a[5], length, i, j;
    
    
    /* initalize the array in case the user is lazy 
       and just press enter */
    for( i = 0 ; i < 5 ; i++)
       a[i] = 0;
    printf("Enter the text : ");
    fgets(s, sizeof(s), stdin); /* it adds '\n' to the string */
    /* scanf("%s",s); */
    i = j = length=0;
    
    while(s[i]!='\0')
       {
       if(s[i] == ' ') /* should be '==' and not '=' */
          i++;
       else
          { /* should be in parthince. */
          while( s[i] && s[i]!=' ') /* check for overflow  in s */
             {
             length++;
             i++;
             }
          a[j]=length;
          j++;
          if ( j >= 5 )  /* check for overflow in a */
             break;
          length=0;
          }
       }
    
    for(i=0 ; i < 5 ; i++)
       printf("Word %d : %d characters\n",i+1,a[i]);
    
    return(0);
    }

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    dude543 running your program and testing with a few inputs found that the last word is always miscounted by one extra character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. count words program problem
    By Tony654321 in forum C Programming
    Replies: 8
    Last Post: 10-19-2004, 11:23 AM
  4. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM