Notices
Results 1 to 13 of 13

Thread: How do I write a program to find the word lengths

  1. #1 How do I write a program to find the word lengths 
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13

    So I'm having trouble in starting a program where I have to report the longest and shortest words in its input, along with their lengths. (using a vector)

    I have tried some string operations and vector operations, such as str.length() (for length or size) and max (e1, e2) (to find the longest between two words) , but it doesn't work. I'm not allowed to use the sort function yet. btw

    Any ideas?

    Please Help!


    Reply With Quote  
     

  2.  
     

  3. #2 Re: How do I write a program to find the word lengths 
    Suspended
    Join Date
    Apr 2008
    Posts
    2,176
    Quote Originally Posted by ggordita

    So I'm having trouble in starting a program where I have to report the longest and shortest words in its input, along with their lengths. (using a vector)

    I have tried some string operations and vector operations, such as str.length() (for length or size) and max (e1, e2) (to find the longest between two words) , but it doesn't work. I'm not allowed to use the sort function yet. btw

    Any ideas?

    Please Help!
    I have no idea of the language you are using.

    Can you just run over the information many times?

    If you create two variables, "word" and "word length", run through the whole list with an if statement. That gets the word length, and checks it against the number stored in the variable "word length".
    If the actual current word length is higher or greater then the variable "word length" a number stored in the variable. You just write both variables, with the current information, the word itself and the word length gotten by the function.

    When that loop is finished the variables will have the longest word, and its length stored.

    You can do the same thing for the smallest word, just in reverse.

    You have to check with the language and compiler though. Because some variables are not checked during a loop. They are just initially checked, and the original value is used during the running of the loop. That you have to check out first.

    Sometimes with different languages you have to create a static variable, that is checked at every step of the loop. Then transfer the static variable to another type of variable or container, for further processing or storage.

    Sincerely,


    William McCormick


    Reply With Quote  
     

  4. #3  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,810
    as William said, depends on what language you work in

    Java : str.length();
    JavaScript : str.length;
    VB6 : Len(str)
    C# : str.length;
    PHP5 : strlen($str);

    for maximum value :

    Java : Math.max(e1,e2);
    JavaScript : Math.max(e1,e2);
    VB6 doesn't appear to have a max function, but you should be able to create your own simple function
    C# : Max(e1,e2);
    PHP5 : max($e1,$e2);
    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

  5. #4  
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    Yes it's c++ I'm sorry
    Reply With Quote  
     

  6. #5  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,810
    and googling for "string length" and "maximum of two values" doesn't give a satisfactory answer ?

    in which case, may i suggest you try out the C programming part of DevShed (which also covers C++) ? you're more likely to get a satisfactory reply there
    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

  7. #6  
    Suspended
    Join Date
    Apr 2008
    Posts
    2,176
    Quote Originally Posted by ggordita
    Yes it's c++ I'm sorry

    It is easy enough to show you this little portion of what you want to do at one point in your program.

    However, even though I have written programs in "C" and "C++", Visual Basic, Basic, I hate the way you have to handle arrays and variables. And the input and output of files.

    You still need to open a file, read the file and decide how you want to process the information in the file. Then loop the information and find the highest and lowest amount of letters in the words you are processing. And save the word and the number of letters that correspond to that word. For both the largest and smallest word.

    I do not think like that naturally so although I can do it. It usually turns into a big project for me.





    "C" has the "max" function. It will find the highest of two variables being inspected. But you have to create some sophisticated system, basically an array to keep or pin objects to one another.


    Sincerely,


    William McCormick
    Reply With Quote  
     

  8. #7  
    Suspended
    Join Date
    Apr 2008
    Posts
    2,176
    The code does function you have to adjust it to the current platforms though.

    This is a video of the interface with the working code.

    http://www.Rockwelder.com/Computers/...ringlength.htm

    The latest compilers do not just eat that code I gave you in the previous post.

    You have to make a slight alteration to the code for it to work within the current micro soft C++ programing interface.

    I have not tried a Borland compiler for years. I purchased one of Borland's first DOS platforms in the late eighties.
    The last time I sent for a trial, copy to test out. It took me twenty e-mails back and forth before I realized that a faulty installer had locked out any trail version from my computer. What a waste of time. I just wanted to get a feel of the interface.


    This is the header file or .h file content necessary to reproduce the intent of the code I gave you in the previous post. I named the header file "length.h" I refer to the header file in the cpp file.

    iostream.h and string.h are library files. That contain code, and functions that make the file work. There are many different libraries and you can write your own and include them into your program.

    #include <iostream>
    #include <string>




    This is the .cpp file that you would need to create, to recreate the intent of the code I gave you.


    #include "length.h"
    using namespace std;
    int main()
    {
    char buffer[80];
    do
    {
    cout << "Enter a string up to 80 characters: ";
    cin.getline(buffer,80);
    cout << "Your string is " << strlen(buffer);
    cout << " characters long. " << endl;
    } while (strlen(buffer));
    cout << "\nDone. " << endl;
    return 0;
    }



    Here is a link to a video from Microsoft about their interface/compiler.
    http://www.microsoft.com/resources/m...008Express.wvx

    It has some good tips about the interface.

    You can get a trail of C++ 2008 from microsoft. It works, I just downloaded it and tried it out.

    http://www.microsoft.com/express/vc/


    Sincerely,


    William McCormick
    Reply With Quote  
     

  9. #8 hooray 
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    I actually got it, guys.
    I didn't need a vector. I overcomplicated it wit that. Thanks to everyone for their help!
    If anyone needs the code, I'll be happy to share it!
    Reply With Quote  
     

  10. #9 Re: hooray 
    Suspended
    Join Date
    Apr 2008
    Posts
    2,176
    Quote Originally Posted by ggordita
    I actually got it, guys.
    I didn't need a vector. I overcomplicated it wit that. Thanks to everyone for their help!
    If anyone needs the code, I'll be happy to share it!
    Sure I would love to see it.


    Sincerely,


    William McCormick
    Reply With Quote  
     

  11. #10 Re: hooray 
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    Quote Originally Posted by William McCormick
    Quote Originally Posted by ggordita
    I actually got it, guys.
    I didn't need a vector. I overcomplicated it wit that. Thanks to everyone for their help!
    If anyone needs the code, I'll be happy to share it!
    Sure I would love to see it.


    Sincerely,


    William McCormick
    #include <iostream>
    #include <string>

    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;


    int main()
    {

    string s;
    cin >> s;

    string longest = s;
    string shortest = s;

    int longestSize = s.length();
    int shortestSize = s.length();

    if (cin == NULL) {
    cout << "No input. Try again." << endl;
    return 1;
    }

    // loop invariant: got longest and shortest so far, adjust longest
    // and shortest until the end of loop
    while (cin >> s) {

    // compare strings
    // find the longest string

    if (s.length() > longestSize) {

    longest = s;
    longestSize = s.length();
    }
    // find the shortest string
    else if (s.length() < shortestSize) {

    shortest = s;
    shortestSize = s.length();
    }

    }

    cout << "Longest word is \"" << longest
    << "\", length: " << longest.length() << endl;
    cout << "Shortest word is \"" << shortest
    << "\", length: " << shortest.length() << endl;


    return 0;
    }


    What do you think?
    Reply With Quote  
     

  12. #11 Re: hooray 
    Suspended
    Join Date
    Apr 2008
    Posts
    2,176
    Did you try to compile and run that?

    I had to alter it a bit to compile. But I left the basic code as it was. It did compile however it does not seem to do anything after it runs.

    I am not sure how you are declaring your variables. I do not see it working like that. But I am not an expert.


    Sincerely,


    William McCormick
    Reply With Quote  
     

  13. #12 Re: hooray 
    Forum Freshman
    Join Date
    Jan 2009
    Location
    new york
    Posts
    13
    Quote Originally Posted by William McCormick
    Did you try to compile and run that?

    I had to alter it a bit to compile. But I left the basic code as it was. It did compile however it does not seem to do anything after it runs.

    I am not sure how you are declaring your variables. I do not see it working like that. But I am not an expert.


    Sincerely,


    William McCormick
    Well what I do is run the program, type in some input, then do the command that means "end of input"--in my program its control-d. it works
    Reply With Quote  
     

  14. #13 Re: hooray 
    Suspended
    Join Date
    Apr 2008
    Posts
    2,176
    Quote Originally Posted by ggordita
    Quote Originally Posted by William McCormick
    Did you try to compile and run that?

    I had to alter it a bit to compile. But I left the basic code as it was. It did compile however it does not seem to do anything after it runs.

    I am not sure how you are declaring your variables. I do not see it working like that. But I am not an expert.


    Sincerely,


    William McCormick
    Well what I do is run the program, type in some input, then do the command that means "end of input"--in my program its control-d. it works

    All I have loaded on my machine right now, is Microsoft C++ 2008 Express edition. I have an older copy of Microsoft C++ somewhere. If I get a chance I will load it and see if it compiles in that.

    But it does not compile as it is in Microsoft C++ 2008.

    Sincerely,


    William McCormick
    Reply With Quote  
     

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •