How to display the longest word in a string.

In my code, I am trying to display the longest word within my text file. I've seen been successful in actually getting the program to read and output a word but it's far from desired. I'm using a string function to read the text in the data file and out of all the terms the only one being output is "end" when the desired term is a 16 character term. I'm not sure if the problem stems from how I have my code setup or if I have implemented the function incorrectly into my main function. Some advice/guidance on how to fix my problem without the use of arrays or vectors would be greatly appreciated.

String Function:

javascript:tx('code')
string Longest_Word(string big)
{
string tmpWord = "";
string maxWord = "";

getline(cin, big);

for (int t = 0; t < big.length(); t++)
{
if(big[t] != ' ')
{
tmpWord += char(big[t]);

}
else
tmpWord = "";

if(tmpWord.length() > maxWord.length())
{
maxWord = tmpWord;
}
return big;
}
}

cout << big << endl;

}

javascript:tx('b')
String function implemented into main function:

javascript:tx('code')
(*texts is string variable used to read text in data file*)
texts = Longest_Word(word);


cout << "Longest word before formatting: ";
cout << texts;
cout << endl;

The easy way is to give std::max_element stream iterators and a comparison function that compares length. No vectors or arrays - is this an acceptable solution?
http://en.cppreference.com/w/cpp/algorithm/max_element
.
In your function you appear to be passing a string into the function then replacing its content with cin. If you are reading a text file then use ifstream. There are plenty of tutorials online, so I'll leave the googling to you.

To get words from a stream one at a time use the formatted input operator >> in either cin or ifstream. The operator will handle all whitespace including tabs and internal newline characters.

You could put an existing string into a stringstream which you could then use the formatted input operator just as if it were a regular input stream like cin or ifstream.

Your return statement should be outside of the for loop and return maxWord instead of big.
Last edited on
I appreciate the advice with using ifstream however, I have a constraint to my program reading the data file through linux redirection. So while I tried this and it worked without a problem. It is still a solution I cannot use.
std::ifstream is an std::istream. std::cin is an instance of std::istream -- they're interchangeable here.
See, e.g.,:
1
2
3
4
5
6
7
8
9
10
11
# include <iostream>
# include <iterator>
# include <algorithm>

int main() { 
    using Iter = std::istream_iterator<std::string>;  
    auto longest_token = std::max_element
        (Iter{std::cin}, Iter{}, [](auto const& a, auto const& b)
         { return a.size() < b.size(); });
    if (longest_token != Iter{}) std::cout << *longest_token << '\n';
}

http://coliru.stacked-crooked.com/a/e005ba359706f3c5
How I would do this:

1. Read the file into a string line by line.

2. Replace all non-alphabetic characters with spaces.

3. In a while loop read the words one by one with a stringstream (possible since now there are only alphabets and spaces) and find out the greatest length() of all the strings.

4. Add only the strings that have the greatest length() into std::vector, std::set, or some other container.

5. Using std::max_element get the alphabetically greatest string out of all these (all should have same length).

Now you have the longest and alphabetically greatest string from the text file.

Note: You could combine steps 1 & 2.
Last edited on
@TAdgoke

You should say who's post answered your question, why was it helpful, and how it solved your problem so other people who has a similar problem can see this.
Topic archived. No new replies allowed.