Finding the longest word in a string.

This is a discussion on Finding the longest word in a string. within the C Programming forums, part of the General Programming Boards category; It it simple to find the number of characters in the longest word, but I am having difficulty printing the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    1

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21