For developers

C++ character counter: count the characters in a string

Ready-made C++ code samples for counting characters: all characters, characters without spaces, and the word count. Plus important UTF-8 notes. You can also test the counting right in your browser.

✓ Copy-paste-ready code✓ UTF-8 notes✓ Test in your browser
Characters with spaces 0 all visible characters + spaces
Characters without spaces 0 spaces removed
0
Words
0
Sentences
0
Paragraphs
0
Lines
Your text is never stored or sent to a server

Character limit or target

Custom:
0 / 0 Pick a limit or enter your own

Counted with spaces. Change the basis with the switch below.

Character limits by channel (characters with spaces)

Show text analysis
Avg. word length
0
Words / sentence
0
Characters / sentence
0
Unique words
0
Lexical diversity
0
Estimated reading time
0 s
Estimated speaking time
0 s
Longest word
–
Spaces
0
Line breaks
0
Tabs
0
Punctuation
0
Numbers
0
Links
0
Emojis
0

Most frequent words

  • Type some text to see the most frequent words.
Advertisement

1. All characters in a string

The basic way to count characters in C++ is the size() or length() method of the std::string class. They return the length of the string in bytes. In pure ASCII text the byte count equals the number of characters.

characters.cpp
// All characters (in bytes)
#include <iostream>
#include <string>

int main() {
    std::string text;
    std::getline(std::cin, text);
    std::cout << "Characters: " << text.size() << "\n";
    return 0;
}

2. Characters without spaces

When you want to count only the actual characters, iterate over the string and skip whitespace with the std::isspace function. This gives you the equivalent of the page counter's no-spaces figure.

without_spaces.cpp
// Characters without spaces
#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string t; std::getline(std::cin, t);
    int n = 0;
    for (unsigned char c : t)
        if (!std::isspace(c)) n++;
    std::cout << "Without spaces: " << n << "\n";
    return 0;
}

3. Word count

Words are conveniently counted with a std::istringstream stream, which splits the text into words at the spaces.

words.cpp
// Word count
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string line; std::getline(std::cin, line);
    std::istringstream iss(line);
    std::string word; int words = 0;
    while (iss >> word) words++;
    std::cout << "Words: " << words << "\n";
    return 0;
}

UTF-8: bytes are not the same as visible characters

This is the biggest pitfall with non-ASCII text. std::string::size() returns the byte count, not the number of visible characters. In UTF-8 encoding, letters like é, ñ and ö take two bytes and many emoji take as many as four. That is why the word "café" can look like five bytes even though it has four visible characters.

Remember: if you want to count the visible characters in UTF-8 text, you have to interpret the encoding — for example, count only the bytes that don't start with the bit pattern 10xxxxxx. The in-browser counter above uses grapheme-based counting, so you can compare its result with your code.

Advertisement

Recent counts

Counts are saved only in your own browser so you can return to them later. They are never sent to any server.

    FAQ

    Frequently asked questions

    How do I count the characters in a string in C++?

    The simplest way is to use the size() or length() method of the std::string class. They return the length of the string in bytes. In ASCII text this equals the number of characters, but in UTF-8 text accented letters and emoji can take several bytes.

    How do I count characters without spaces?

    Iterate over the string and skip spaces, for example by checking isspace(). You can also remove just ordinary spaces or all whitespace, depending on what you need.

    Why are accented characters counted incorrectly?

    std::string measures the length in bytes. In UTF-8 an accented character takes two bytes, so the number of visible characters and the byte count differ. Counting visible characters requires encoding-aware handling.

    Can I try the counting without a compiler?

    Yes. Paste the string into the counter above and you'll see characters with and without spaces right in your browser. This lets you compare the result with your C++ code's output.