0

How can I count the letters of a word?

I'd like to count the letters of a word. but It don't works. Please help me

21st Nov 2016, 1:07 PM
niklas opiela
niklas opiela - avatar
7 Respuestas
+ 3
If I understand your problem. you mean length of word, you can use it like string s; s="Hello"; cout << s.length() << endl; cout << s.size(); output: 5 5 if not please explain your problem with an example.
21st Nov 2016, 1:16 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 2
Here it is coded with cin asking to input a word... hope this helps... #include <iostream> using namespace std; int main() { string s; cout << "Please enter a word:" << endl; cin >> s; cout << "The word entered was: " << s << endl; cout << s.length() << endl; cout << s.size(); }
21st Nov 2016, 1:52 PM
Matthew Horn
Matthew Horn - avatar
+ 2
You can declare a string like I saw in another example: string s; s="Hello"; So let's state the obvious and string s' length is 5. So You can declare a string like I saw in another example: string s; s="Hello"; But generally a non dynamic number needs a length or count since we already know what the length is. So when someone inputs something, like their username which needs to be a minimum of 6 chars(just an example) we can do a similar code. string s; cout << "Enter Username" << endl; cin s; //just outputting the username and length, you can obviously do lots more! cout << "Your username:" cout << s << "is over:" cout << s.length() << " characters and is valid!" I tried to break it down section by section. might have been a little overkill but hopefully it works well for you. I didn't add the # include <iostream>.... etc at the top I'm sure you got that part. I apologize if I'm missing a char here or there I'll edit it I just went from memory/I saw one typo already.
22nd Nov 2016, 2:46 AM
Matt James
Matt James - avatar
+ 1
Aditya was correct, although not thorough... #include <iostream> using namespace std; int main() { string s; s="Hello"; cout << s.length() << endl; cout << s.size(); } You can also change that to accept user input with a cin as well. There is also the ability to read from a file.
21st Nov 2016, 1:31 PM
Matthew Horn
Matthew Horn - avatar
0
Matthew can I do it like my way?
21st Nov 2016, 1:35 PM
niklas opiela
niklas opiela - avatar
0
that doesn't work... the word is a string first off...
21st Nov 2016, 1:44 PM
Matthew Horn
Matthew Horn - avatar
- 2
#include <iostream> #include <set> using namespace std; int wort; int lcd; int main(){ cin>>wort; lcd = count (wort.begin()wort.end()); cout<<lcd; return 0; } This is my Sketch
21st Nov 2016, 1:28 PM
niklas opiela
niklas opiela - avatar