+ 1
If i have a string like "this is string" i want to konw how many words and how many letters in c++ [SOLVED]
6 ответов
+ 5
string str = "this is string";
int numOfLetters = str.length();
int wordCount = 0;
for(int i = 0; i < numOfLetters; i++){
if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
{ wordCount++; }
}
wordCount += 1;
cout<<numOfLetters<<endl;
cout<<wordCount;
+ 10
【ACE】 , (AbdElrahman Maans),
you are showing a really nice code to help someone solving a task. we all appreciate this very much!
(this is my personal statement - but it is not to criticize someone !!)
but there is an issue by giving a ready solution, when the op has not done and shown his attempt first.
as a part of the community i believe that learning by doing is more helpful, than just using a shared solution. we should act more as a mentor.
it is a very successful way when we are giving hints and help, so that the op is able to solve his task by himself. it would be great if you are going to help us to keep sololearn what it is:
▪︎a unique place to learn (we are a "self" learning platform!)
▪︎an absolutely great community that is willing to help other people
▪︎a great resource of people which has an unbelievable knowledge in coding and a long lasting experience
▪︎a place to meet people and to discuss with them all the possible points of view
thanks for your understanding
+ 3
Lothar I posted my answer without explanation, since it was a simple code.
he wanted to know how? so i just showed him one of the ways of doing it, the code may not be good, but he will get the idea.
+ 2
Share your code link in the Description ☝
People are more eager to help when they see you tried ...
https://www.sololearn.com/post/75089/?ref=app
+ 2
You can use strtok()...Google it, happy coding 😄
0
Use a loop