count #of words and characters..!!
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
int main() {
unsigned long i=0;
unsigned long len=0;
unsigned long st_pos=0;
unsigned long i_init = 0;
unsigned long index = 0;
string s = "this example has multiple lines";
vector<string> word;
while(true){
if(index >= s.size()-1) break;
if(s[index] != ' ') {
for(int a = index+1; a << s.size();a++) {
if(s[a] == ' '){
st_pos = index;
len = a-index;
word.push_back(s.substr(st_pos,len));
index = a;
break;
}
else if(a==s.size()-1){
st_pos = index;
word.push_back(s.substr(st_pos));
index = a;
break;
}
}
}
index++;
}
int num = 0;
int num1 = 0;
for(int i = 0; i<word.size(); i++) {
cout<<word[i]<<"\t"<<word[i].size()<<endl;
num1 += word[i].size();
}
cout<<word.size()<<"\t"<<num1<<endl;
return 0;
}
|
This code suppose to return the #of words and #of characters..!!
However, this returns e 1
1 1
If is assign s = "this is another example", this returns 4 20 which is correct!! can anyone fix this?? and plz explain why!! Thank you!!
Topic archived. No new replies allowed.