How can I improve this code and are there any situations where it won’t work?
<code>#include <string>
int find_short(std::string str)
{
int word_length = 0;
int length = 0;
int length_min = str.size();
for(int i =0; i < str.size(); i++){
if(str[i] == ' '){
word_lehgth = length;
if(word_lehgth < length_min){length_min = word_lehgth;}
length = 0;
continue;
}
length++;
}
return length_min;
}
</code>
<code>#include <string>
int find_short(std::string str)
{
int word_length = 0;
int length = 0;
int length_min = str.size();
for(int i =0; i < str.size(); i++){
if(str[i] == ' '){
word_lehgth = length;
if(word_lehgth < length_min){length_min = word_lehgth;}
length = 0;
continue;
}
length++;
}
return length_min;
}
</code>
#include <string>
int find_short(std::string str)
{
int word_length = 0;
int length = 0;
int length_min = str.size();
for(int i =0; i < str.size(); i++){
if(str[i] == ' '){
word_lehgth = length;
if(word_lehgth < length_min){length_min = word_lehgth;}
length = 0;
continue;
}
length++;
}
return length_min;
}
The program does not always give the correct values
New contributor
Grigoriy Metelyov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2