Counting Letters in Word

Sep 9, 2019 at 10:54pm
I am having troubles how to start creating program which counts letters in words in .txt file and print it if letter number is > 20.. Does someone know how to start or have some similar program to help me..Thank you

Sep 10, 2019 at 1:31am
Does your data file uses spaces as the word delimiter? Contain no punctuation? Just alphabetical letters?

Read each word in your data file into a std::string. The size of the resulting string is the number of letters.

Loop for each word and success.
Sep 10, 2019 at 1:16pm
Just to show an example, because I don't think the tutorial shows a good example that uses >> for input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    // Assumes file has words separated by whitespace
    std::ifstream file("words.txt"); 
    
    // read in a word
    std::string word;
    file >> word; // operator>> separates each word by whitespace
    
    // print length
    std::cout << word.length() << '\n';
}
Last edited on Sep 10, 2019 at 1:18pm
Topic archived. No new replies allowed.