Extracting a particular length of words from the strings

Write your question here.

I am taking a dictionary of words from a text file and want to display all the words with four letters.

1
2
3
4
5
6
7
8
9
10
11
12
13
 

  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;
You'd have to open a file, to begin with.

Hint: The following will loop through every space-delimited word in a file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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;
    }
 
}
Last edited on
I got this, but my question is how to extract the words of particular letters length
A really good reply here:

http://www.cplusplus.com/forum/general/232960/#msg1047961

Surely you can use an if statement?

Duplicate post:

http://www.cplusplus.com/forum/general/233095/

Asking the same question several times won't necessarily help you get an answer, and is a time waster for those who respond. Just keep the same topic going.
Topic archived. No new replies allowed.