Thread: Problem with the code to count the word length if you have many words (sentence)

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    Problem with the code to count the word length if you have many words (sentence)

    I have problem to write code to following example:

    print on screen - Enter setence (some text)
    read from screen - setence (text)

    While - not end of file (or something like that)

    how many characters ? have the longest word (from setence)

    print on screen "Longest word have XX characters"

    grateful if you can help me and show me the code for this problem, (I tried for hours with no success)

    oldman from Europe

  2. #2
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    You could make use of strtok() to tokenize the sentence into separate words and then use strlen() to determine which is the longest.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I have problem to write code to following example:
    So show us which bits you can do (like print a prompt, read a line) and then comment where the bits you can't do should go.
    That way, we don't explain stuff you already know.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    I have tried this following code with errors but I must have mistaken something: (you can improve it)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLEN 100
    #define TRUE 1
    
    int main(void)
    {
      
      char string[MAXLEN];
      
      int i, j, length = 0, maxlength = 0;
      
      system("cls");
      
      printf("Enter your name, address, city and state or some text\n");
      gets(string);
     
         
      while(i = 0; string[i] != '\0'; i++)
      {
       
        
        for(j = 0; string[j] != ' '; j++)   *I think this is not right*
        {                                                    
          length++;
          if(length > maxlength)
            maxlength = length;
        }
       
       string[j]++;
      
      }
       
     
      printf("Longest word have %d chracters\n", maxlength);
      
      system("PAUSE");
    
      return 0;
    }
    Last edited by Salem; 09-28-2004 at 12:33 PM. Reason: Damn the lack of code tags!!!!!!

  5. #5
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Ok, this is what I meant:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define DELIM " \n\t"
    
    int main(void)
    {
        char input[BUFSIZ];
        printf("Enter a sentence: ");
        fgets(input, sizeof(input), stdin);
    
        char *token = NULL;
        token = strtok(input, DELIM);
    
        int len = 0;
        char *longest = NULL;
        while (token != NULL)
        {
            if (strlen(token) > len)
            {
                len = strlen(token);
                longest = token;
                token = strtok(NULL, DELIM);
            }
        }
        printf("Longest word: %s has %d characters.\n", longest, len);
        return EXIT_SUCCESS;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Meh, Nessarose is posting C++ code on the C board.

    > gets(string);
    Use fgets() as Nessarose has done.

    > while(i = 0; string[i] != '\0'; i++)
    You're mixing a while loop and a for loop here.

    > length++;
    Looks good.
    But you need to reset it back to 0 when you reach the end of a word.

    > string[j]++;
    Why would you want to increment a character in the string?

    Code:
    while ( string[i] != '\0' ) {
      while ( string[i] != ' ' && string[i] != '\0' ) {
        // something
        i++;
      }
    }
    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. Begginer Problem: Extracting words from sentence
    By barlas in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 03:17 PM
  2. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM