Thread: Query about strings

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Query about strings

    I'm having trouble making a loop that would count letters of an array until it reaches the end of the array, then display the length of the largest word in that array. For example:

    array[14] = { "h", "a", "p", "p", "y", " ", "b", "i", "r", "t", "h", .... }

    you get the point. My question is, how can I fix this loop so it reads not only the first word, but the second word as well? Below is my version, which runs improperly.


    while ( string[j] != '\0' ){

    if ( string[j] != ' ' )
    {
    counter++;
    j++;
    }
    else if ( counter > bignum )
    {
    bignum = counter;
    counter = 0;
    j++;
    }
    else
    j++;

    }

  2. #2
    Unregistered
    Guest
    You don't have enough "cases". Compare below with your code.

    int bignum = 0;

    while ( string[j] != '\0' ){

    if ( string[j] != ' ' )
    {
    counter++;
    j++;
    }
    else if (sting[j] == ' ' && counter > bignum )
    {
    bignum = counter;
    counter = 0;
    j++;
    }
    else if(string[j] == ' ' && counter <= bignum)
    {
    counter = 0;
    j++;
    }
    }

    cout << "the longest word was " << bignum << " letters long." << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM