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
7 Réponses
+ 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.
+ 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();
}
+ 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.
+ 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.
0
Matthew can I do it like my way?
0
that doesn't work... the word is a string first off...
- 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