counting characters per line

hey guys am trying to count the number of letters in each line e.g
the following input
like
beats
output should count as follows
4
5
here is what i have so far......my vector contains the inputs.....please help i dont know where to start to output...thanx in advance



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 #include <vector>
#include <cstring>
#include <sstream>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream infile("input.txt");
    string line;
    string buf;


    vector<string> tokens; // Create vector to hold our words
while(getline(infile,line))
{
    istringstream ss(line);
    while (ss >> buf)
        tokens.push_back(buf);

}
//////////////////////////


}
Last edited on
You could iterate through the vector, displaying the length of each string element with the length(); member function.

Aceix.
can u plzzz explain more clearly with an example am sorry just new in c++
Something like this:

1
2
3
4
5
6
7
8
9
//C++11 code
vector<string> vec{"peace","war"};
for(auto const& x:vec) {
  cout<<x.length()<<endl; //this gives the length of the words/strings
}
/*the loop is equivalent to
for(vector<string>::const_iterator it=vec.cbegin();it!=vec.cend();it++) {
  cout<<x.length()<<endl;
} */


...oh, so this basically goes through the vector(all elements of it), and displays the lengths of the words stored in them.

Check:
http://www.cplusplus.com/reference/string/string/

Aceix.
Last edited on
Topic archived. No new replies allowed.