Thread: Help with getting longest string

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Question Help with getting longest string

    I am doing C++ programming course and need some help on this assignment.
    The assignment is to get strings from the user until end comes up and then return the longest string i am stuck and not sure where to go. Here is the little that I have done.
    /* File: longStr.cpp
    * This program reads a list of words until "end"
    * appears and return the longest string.
    */

    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    #include "strlib.h"

    main()
    {
    string firStr, lonStr;
    int num, lon;

    printf("Please enter list of strings.\nAs a sentinel value use end\n");
    firStr=GetLine();
    lon=0;

    while(!(StringEqual(firStr, "end")))
    {
    firStr=GetLine();
    num=StringLength(firStr);
    lon=(lon>=num)? lon : num;
    printf("%d\n", lon);
    }
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    while(!(StringEqual(firStr, "end")))
    You shouldn't need to use a function like that; for C++ strings, just use ==, and for C strings, you can use strcmp(). (Unless StringEqual does in-case sensitive comparison.)

    You might use something like
    Code:
    if(this string is the longest string) {
        set the longest string to this string
    }
    
    // ...
    
    print the longest string
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> lon=(lon>=num)? lon : num;
    You should get rid of the ternary operator ( ? : ) and use an if statement instead. Then you can remember the lonStr inside the if statement if the length is actually longer.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't have to eliminate the ternary operator. Anything that can be done with an if+else can be done with a ternary operator (although it becomes unwieldy for miltiple lines ). You could change it to:
    Code:
    lonStr = (num > lon ? firStr : lonStr);
    Of course, it would be much simpler to just use
    Code:
    if(num > lon) lonStr = firStr;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Not sure if this will help but would it not be wise to use a vector of strings? And just keep pushing bak until the string is equal to end.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That would be a waste of time and space if all you're trying to do is figure out the longest line.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Oh right I see wat he has done, yes woops...

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    see, the thing is i have to print the string that is the longest. NOT the length of the longest string. Srry about confusement, or am i just being stupid?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's what I posted. Well, what I should have posted anyway.
    Code:
    if(num > lon) {
        lonStr = firStr;
        num = lon;
    }
    If the length of the previously longest string is less than the length of the current string, set the longest string to the current string and set the length of the longest string to the length of the current string. (Whew. C++ is more consice than English.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    K got that but still need program to end when user types in end. Sorry i am new with strings. i am using while like this.
    While(firStr!="end")
    ...

    Is that right?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You don't have to eliminate the ternary operator.
    As you later realized, the if statement is the proper way to go. Even if it is possible to do both assignments with the ternary operator, there would be no reason to create such confusing and tricky code.

    >> Is that right?
    Yes, but you will have a small problem that end will be checked as well. So if the user types in:
    Code:
    it
    be
    is
    a
    end
    your program will mistakenly output "end". To fix it, just check for "end" before doing the logic that checks the string to see if it is the longest.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, an if statement is much cleaner since you don't even need an else.

    Quote Originally Posted by bosredsox247
    K got that but still need program to end when user types in end. Sorry i am new with strings. i am using while like this.
    While(firStr!="end")
    ...

    Is that right?
    Yes, that would work (assuming a lowercase 'w' in "while").
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    then why isnt it? Some stupid mistake probably.
    By the way, thanks for your time and help.
    main()
    {
    string firStr, lonStr;
    int num, lon;

    printf("Please enter list of strings.\nAs a sentinel value use end\n");
    lon=0;
    firStr=""
    while(firStr!="end")
    {
    firStr=GetLine();
    num=StringLength(firStr);
    if(num > lon) {
    lonStr = firStr;
    num = lon;
    }
    }
    printf("The longest string is: %s\n", lonStr);

    }

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Missing semicolon:
    Code:
    firStr=""
    Code:
    num = lon;
    I put that in to trick you. (Actually it was an oversight. ) It should be the other way around.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    this is ........ing me off just as much as you. y the heck isnt it working.
    i did wat u said. And once again it isnt working. SRRY
    main()
    {
    string firStr, lonStr;
    int num, lon;

    printf("Please enter list of strings.\nAs a sentinel value use end\n");
    lon=0;
    firStr="";
    while(firStr!="end")
    {
    firStr=GetLine();
    num=StringLength(firStr);
    if(num > lon) {
    lonStr = firStr;
    lon = num;
    }
    }
    printf("The longest string is: %s\n", lonStr);

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM