Reputation:
I need a help. I have a function which print Longest Word in sentence. But how to display shortest word?
string text = "My name is Bob";
void LongestWord(string text)
{
string tmpWord = "";
string maxWord = "";
for(int i=0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
else
tmpWord = "";
/// All the time check word length and if tmpWord > maxWord => Rewrite.
if(tmpWord.length() > maxWord.length())
maxWord=tmpWord;
}
cout << "Longest Word: " << maxWord << endl;
cout << "Word Length: " << maxWord.length() << endl;
}
Upvotes: 0
Views: 9974
Reputation: 96845
void ShortestWord(std::string const& text)
{
std::stringstream ss(text);
std::vector<std::string> v(std::istream_iterator<std::string>(ss), {});
auto min = std::min_element(v.begin(), v.end(),
[] (auto& lhs, auto& rhs) { return lhs.size() < rhs.size(); });
auto p = std::make_pair(*min, min->size());
std::cout << "Shortest Word: \"" << p.first << "\"\n";
std::cout << "Word Length: " << p.second << '\n';
}
Upvotes: 1
Reputation: 278
If we want to get both min value and max value, the initialize values should be opposites to each of them.
Actually, that should be the max-limit string of 'text'.
In the development of the business application, this is common sense, but some programers may hate the way of this.
string minWord = text; // MAX_SIZE
string maxWord = "";
for(int i = 0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
if(text[i] == ' ' || i == text.length()) {
/// All the time check word length and if tmpWord > maxWord => Rewrite.
if(tmpWord.length() > maxWord.length())
maxWord = tmpWord;
if(tmpWord.length() < minWord.length())
minWord = tmpWord;
tmpWord = "";
}
}
Upvotes: 0
Reputation: 2233
The suggestion given in the comment section will work, it's just a matter of rearranging your control structures to make it work. i.e
for(int i=0; i < text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
tmpWord += text[i];
else
{
if(minWord.length()==0)//this only happens once
minWord=tmpWord;//for the first word,you need to assign minWord so you have something to compare to
if(tmpWord.length() < minWord.length() )//move this block here
minWord=tmpWord;
tmpWord = "";
}
}
I might add, you can check for a word much easily if you used istringstream
with the extraction operator>>
. Something like:
#include <sstream>
....
string text="my name is bob";
string tmpWord = "";
string minWord = "";
istringstream ss(text);//defines the input string stream and sets text in the input stream buffer
while(ss.peek()!=EOF)//until the end of the stream
{
ss>>tmpWord;//read a word up to a space
if(minWord.length()==0)//this only happens once
minWord=tmpWord;
if(tmpWord.length() < minWord.length() )
minWord=tmpWord;
}
Upvotes: 1
Reputation: 26
void ShortestWord(string text)
{
string tmpWord = "";
// The upper bound of answer is text
string minWord = text;
for(int i=0; i < (int)text.length(); i++)
{
/// If founded space, rewrite word
if(text[i] != ' ')
{
tmpWord += text[i];
}
else
{
// We got a new word, try to update answer
if(tmpWord.length() < minWord.length())
minWord=tmpWord;
tmpWord = "";
}
}
// Check the last word
if(tmpWord != "")
{
if(tmpWord.length() < minWord.length())
minWord=tmpWord;
}
cout << "Shortest Word: " << minWord << endl;
cout << "Word Length: " << minWord.length() << endl;
}
Upvotes: 0