Arrays: I have a word in an array as an element, how do I count the letters?

Homework help

I have a word pulled from a text file and put into an array. How do I then load that word into a separate array, so that I can count the number of letters in the word?

1
2
3
4
5
6
7
ifstream infile;
infile.open ("words.txt")

infile>>testword [1]>>testword [2];// I am guessing that testword [1] puts the whole word into the element 1.

cout<<testword [1];//displays whatever the first word in the text file was.


I now want to enter testword [1] into an array so that each letter is an element....any ideas how to do this?
Last edited on
do you really need the struct? It's pretty trivial to write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// in main
       string input1, input2
       vector<string> firstWord;
       // open file 
       while(/* file is good */)
       {
             fin >> input1 >> input2;
             firstWord.push_back(input1);
       }
        
       // later just display the vector
       for( int i = 0; i < firstWord.size(); ++i )
       {
             cout << firstWord[i] << endl;
       }

I don't need the struct, but we aren't using vectors yet.
if you're not using a container then it's easier, just read the file and output the first word read, refer to my example and print out input1

edit: typo
Last edited on
ack, didn't realize you've changed the topic (and rewritten the question -bad practice there, mate!)

counting letters in words is easy, e.g
1
2
3
4
5
6
         string w[4] = { "this", "is", "an", "example" }; // array of words
         
         for(int i = 0; i < 4; ++i)
         {
              cout << w[i] << " contains " << w[i].size() << " letters" << endl;
         }



this contains 4 letters
is contains 2 letters
//etc
edit: typo
Last edited on
Topic archived. No new replies allowed.