Thread: Longest and Shortest String

  1. #16
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by NeonBlack View Post
    or you could have used else?

    Yes but else make the code look complex, that's why I didn't want to use it.
    It looks a lot neater with a continue, and easier to read I think.

    Quote Originally Posted by NeonBlack View Post

    Code:
    x_len=x.length();
    This is not necessary as it is with strlen() because string.length() has constant time.

    Do you mean that it remembers the length?
    How would it know x had not changed?

  2. #17
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by KIBO View Post
    you could also just remember the indexes of the shortest and longest string and in the print. No copying of strings then.

    Code:
    int shortest = 0, longest = 0;
    
    for (int i = 1; i < words.size(); i++)
    {
      const string &word = words[i];
    
      if (word.length() < words[shortest])
        shortest = i;
      if (word.length() > words[longest])
        longest = i;
    }
    
    cout << words[shortest] << endl;
    cout << words[longest] << endl;
    You have to remember the contents of the longest and shortest strings inorder to print them, with an index you have to remember all the strings.

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    wow, I had no idea this post was still ongoing lol. Here is my code, yours is definitely better esbo.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::vector;
    using std::string;
    
    
    int main(){
        //asks for list of words
        cout << "Please give me a list of words:" << endl;
        
        string x;
        vector<string> words;
        
        //adds the input to the vector
        while (cin >> x){
              words.push_back(x);
              }
        
        //checks to see if user inputted anything
        if (words.size() == 0){
           cout << "I didn't receive any input." << endl;
           
           cout << "Press any key to continue...";
           _getch();
           return 1;
           }
        
        //the containers
        string longest = words[0];
        string shortest = words[0];
        
        //checks for the shortest and longest string
        for(int i = 1; i != words.size(); i++){
                if(words[i].length() < shortest.length()){
                                     shortest = words[i];
                }
                if(words[i].length() > longest.length()){
                                     longest = words[i];
                }
                
        }
        
        //the output
        cout << "Shortest: " << shortest << endl;
        cout << "Longest: " << longest << endl;
       
        cout << "Press enter to continue...";
        _getch();
        return 0;
    }

  4. #19
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I got my 'big' C program working in C++, it seems the 'bug' already existed ( I had done a little processing when I should have checked for end of file first) however it never caused any harm to the C program, however as I had rearranged the position of some functions to get it to compile I think that may have made a harmless action dangerous.
    Any by converting it to compile with C++ I uncovered 2 hide bugs, which can't be a bad thing.
    I might write the output section in C++ (cout) to make it easier to modify but I am not familiar with the print format of cout yet, if it's the same as printf Iprobably won't bother.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    hey guys i have a question, if we also want to output the length of the longest/shortest input, is it just cout << longest/shortest.length? sorry, its probably a noob question..

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    longest.length() will give you the length of the string, yes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, it is shortest/longest.length().

    I might write the output section in C++ (cout) to make it easier to modify but I am not familiar with the print format of cout yet, if it's the same as printf Iprobably won't bother.

    I/O stream format
    flags.

    You may want to start a separate thread on this. In any case formatting with streams is completely different.

    It is awkward to a C programmer, but it is type-safe. E.g you simply can't make the following mistake with std::cout:

    Code:
    int n = 10;
    prinf("%s", n);
    If the awkwardness bothers you (formatting output with format strings is just too convenient) you can download boost and use Boost.Format which gets you the best of both worlds: you can use format string but in a type-safe way.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #23
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    thanks..also i was wondering wat does this part do: _getch()

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    I used _getch() because my compiler closed my cmd window everytime i ran my programs. You can remove that and the cout above it. Don't use it if possible...I read its not exactly industry standard, and affects performance...but atm I'm only running small programs so whatever keeps my cmd window open! =] Its nice to know youre learning c++ too jewelz. Maybe we can learn from each other!

  10. #25
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by esbo View Post
    You have to remember the contents of the longest and shortest strings inorder to print them, with an index you have to remember all the strings.
    so? He already has all the strings in a vector

  11. #26
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by jewelz View Post
    thanks..also i was wondering wat does this part do: _getch()

    Basically it reads a key from the keyboard, without echoing it to the screen so it is effectively, press any key to continue. It is unbuffered so you don't have to press carridge
    return.
    When you program exits the window will close so you need something to stop it closing.
    I usually run mine from a batch file wiith a pause in it to keep the window open.

    I don't think _getc will effect performance at all as it is just waiting for a signal that a key has been pressed.
    Last edited by esbo; 01-28-2009 at 09:25 AM.

  12. #27
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by KIBO View Post
    so? He already has all the strings in a vector
    True, but it is not necessary to remember them all, just the longest and shortest as they are the only ones required in the output.

  13. #28
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    hey guys, jus wanna say thanks for the help. i really appreciate it =)

  14. #29
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    ok this is probably the most retarded post ever, but how do i make the output come out within quotation marks?

    for example if input was: Hello I am nice.

    output:

    Longest: Hello
    Shortest: I

    but i need it to be:

    Longest: "Hello"
    Shortest: "I"

  15. #30
    Registered User
    Join Date
    May 2008
    Posts
    15
    Code:
    cout << "Shortest: " << "\"" << shortest << "\"" << endl;
    cout << "Longest: " << "\"" << longest << "\"" << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text processing assignment
    By nellosmomishot in forum C Programming
    Replies: 28
    Last Post: 11-25-2008, 03:56 PM
  2. working on a string tough string prog, need help
    By Brokn in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 04:14 PM
  3. using strlen and finding shortest and longest words
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-30-2001, 06:09 PM
  4. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM