Thread: Printing out 2 largest words

  1. #1
    Registered User
    Join Date
    Apr 2004

    Printing out 2 largest words

    The program is meant to read text input and simply print out the lengths of the 2 longest words and the actual words themselves. The words, you can assume are no longer than 100 characters. The problem I have is more to do with obtaining the actual string to process (I'm not really flash with strings and co. )

    Heres what I've done so far:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define MAXWORDLEN 100
    int main(int argc, char *argv[])
      char max_word[MAXWORDLEN], sec_word[MAXWORDLEN];
      char current_word[MAXWORDLEN];
      int max_length = 0, sec_length = 0;
      int length = 0;
      int input = 0;
      printf("Input words here: \n");
      do {
      // ****** PROBLEM HERE **********
      while (current_word = gets(stdin)){
        // check if it contains a space, which means its a word
        if (isspace(current_word) == 1)
            // test for length
            for (int i = 0, length = 0; current_word[i] != NULL; i++, length++)
            // if biggest store in max_word[]
            if (length > max_length)
                    sec_length = max_length;
                    max_length = length;
                    strcpy(max_word, current_word);
            // test if second biggest, if yes store in sec_word[]
            else if (length > sec_length)
                    sec_length = length;
                    strcpy(sec_word, current_word);
        printf("Press 1 if you want to exit!\n");
      } while (input != 1);  // terminates when input == 1
      return 0;
    I know that while() line is totally wrong haha, but how could I change it so that it reads a character and checks if the character is a space/tab or not (using isspace()) and then store it in the current_word[] string. If isspace() returns a 1, the rest of the code should work.. unless I missed out something small or big

    Any suggestions would be greatly appreciated!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Lucky for you, there's a FAQ link on exactly this topic.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Blah I could only see the C++ version of doing it when I browsed through the faq but thanks *off to read it*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  2. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  4. New Theme
    By XSquared in forum A Brief History of
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM