How get all the words of same length from a string of thousands of words

Can anyone help with the code to extract the words of particular length from the string of words.

#include <fstream>
#include <iostream>

int main()
{
std::ifstream fin("text.txt"); // assumes you have a file called text.txt filled with words

std::string word;
while (fin >> word)
{
std::cout << word << std::endl;
}
string numWords;
int numLetters;
cout << "Enter number of letters in a word";
cin >> numLetters;

if(numWords.length() == numLetters)
// Iam struck here. Please help if you any alternate code

cout << input << endl;
return 0;

}
@raasilinin

Are you wanting to JUST show the word of the desired length, or do you need to save the list of the chosen length words? If it's just show the word, you first need to request the word length from the user, BEFORE reading the text. Then do something like..
1
2
3
4
5
6
7
8
std::string word;
int word_len;
while (fin >> word)
{
  word_len=word.length();
if(word_len == numLetters)
     std::cout << word << std::endl;
}
Somewhere around the 4th duplicate topic now.
JLBorges did show how to collect words by sizes in http://www.cplusplus.com/forum/general/232960/

Now that you have this "map", you just have to check whether it has a key that equals 'numLetters' and if yes, then print those words.


You did not post any followup questions in that thread and thus we must assume that you did understand JLBorges' example completely and to the finest detail.
Topic archived. No new replies allowed.