How to detect how many letters?

Hello forum, how to detect how many letters has an text? (with spaces)

Example:
random text

11 letters

How to do it in c++? Thanks in advance! I use CodeBlocks 16.02!
closed account (48T7M4Gy)
Hi Cosmin,

Yep it's definitely 11 letters for that one. You'll need to write a few lines of code if you get a lot of those to do.

Best to start with a bit of pseudocode then the C++ code.

Give us an update with any questions/problems when you get to that stage. All the best.
+programmer007
I mean how many letters, now the size of it, but thanks!

+kemort
I want in c++, not i other one's sorry but thanks
Last edited on
closed account (48T7M4Gy)
Sorry Cosmin I thought you were at university:

1
2
3
4
5
6
7
8
9
10
// string::length
#include <iostream>
#include <string>

int main ()
{
  std::string str ("random text");
  std::cout << "The size of str is " << str.length() << " bytes.\n";
  return 0;
}


The size of str is 11 bytes.


This is the sample program that p007 gave you the reference to. Press the wheel in the top right hand corner.

If you want the letter count, excluding spaces use isalpha() http://www.cplusplus.com/reference/cctype/isalpha/ or isspace() http://www.cplusplus.com/reference/cctype/isspace/

isspace is probably more useful because it is more direct (maybe)
Last edited on
+kemort
Dude, i mean, how many characters is in sentence with spaces

Example:
"Windows 10"
There are 10 characters

Also, thanks for that links, those are helping me in another situation :)

I hope someone know how to do it :(
closed account (48T7M4Gy)
how many characters is in sentence with spaces


I know what you want to calculate.

So, if you count the number of spaces in the string and subtract that number from the total string length then you have the answer. You have the program for the total length, and all you have to do is run through each character in the string, if it is a space (or character) you can calculate what you want.

Spaces are the best to use because there might be numbers.

number_of_characters = length - number_of_spaces
one thing to be noted is that size of a char is one byte, so when you are calculating the size you are automatically calculating the number of characters (including the spaces)

ps: @kemort I like the initials p007 :)
Last edited on
closed account (48T7M4Gy)
:)
+p007
Awwwwww, now i get it, if i calculate sentence: "random text", it will say its only 6 letters, how to get the entire sentence?
The size of str is 11 bytes.

this is the output I get executing kemort's code in cpp.sh

why are you getting 6?
Last edited on
closed account (48T7M4Gy)
Put random text in quotes the way I showed you Cosmin.

"random text" see line 7

try it with "random" and then with "text" then "random text"
Last edited on
+kemort +p007
Aww, sorry i didn't check that, i also want to cin the sentence thats why i get only 6, theres how i write:

1
2
3
string str;
cin>>str; (random text)
std::cout << "The size of str is " << str.length() << " bytes.\n";


and i get only 6, how i can fix this?
Thanks in advance
Last edited on
instead of cin>> use cin.getline() or gets()
closed account (E0p9LyTq)
Use std::getline() when you want to get a std::string from the user that includes spaces:
http://www.cplusplus.com/reference/string/basic_string/getline/
It worked, thanks so much!
welcome :)
Topic archived. No new replies allowed.