Thread: Finding the longest word in a string.

  1. #1
    Registered User
    Join Date
    Apr 2007

    Finding the longest word in a string.

    It it simple to find the number of characters in the longest word, but I am having difficulty printing the word with the longest number of characters. Any algorithm suggestions?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you already know it's length, and where it starts, then all you need is a for loop.
    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.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    1) Separate string by words.
    2) For each word, check the length of it.
    3) If the length of the one you're checking > the longest word you already looked at, then save this one to be the longest word.
    4) When you're done searching all of the words, you have the longest word already.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a word in a string.
    By esbo in forum C Programming
    Replies: 15
    Last Post: 08-28-2006, 07:48 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. using strlen and finding shortest and longest words
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-30-2001, 06:09 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM