Character Counter in C++?

c program to count number of characters in a string
c program to count the number of occurrences of a character in a string
count number of unique characters in a string c
c program to count number of characters in a file
program to count number of characters in a file in c++
c program to find repeated characters in a string
c count number of characters in a string
c program to count number of words in a text file

I'm trying to find a way to display all the characters in a string and the number of times they occur.

This is what I have so far:

//Any unused includes are part of the default code

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    string st = "";

    cout << "Input a sentence: " << endl;
    getline(cin, st);
    int index = 0;
    int index2 = 0;
    int counters[26] = {0};
    for(int i = 0; i < st.length(); i++)
    {
        int counter = 0;
        index = st.find(st[i],0);
        for(int j = 0; j < st.length(); j++)
        {
            index2 = st.find(st[j]);
            if(index == index2)
            {
                counter++;
            }
        }
        cout << st[i] << ": " << counter << endl;
    }
    //cout << st[i] <<": " << counters[st[i] - 'a'] << endl;
    return 0;
}

and I return this:

Input a sentence:

hello

h: 1

e: 1

l: 2

l: 2

o: 1

so I kind of have something but I can't figure out how to make the letters not repeat more than once. I know that I need to store them in an array but it's out of my ken.


You were very close, nice try! I liked the approach with the counters array, where every cell would represent the frequency of a letter in the given string.

So, just go and update this array, as this answer implies How to get character's position in alphabet in C language?, without the plus one they mention there, since you want the index of the letter in your array. In other words, for 'a', you need 0, 'b', you need 1 and so on.

Then, in the printing phase, just use the above link's suggestion in the reverse way. When you print the i-th non-zero element of counters, print the i-th element of the element, which will reveal the letter in question.

Putting all together, you get:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    string st = "";

    cout << "Input a sentence: " << endl;
    getline(cin, st);
    int index = 0;
    int index2 = 0;
    int counters[26] = {0};
    for(size_t i = 0; i < st.length(); i++)
    {
        int counter = 0;
        index = st.find(st[i],0);
        for(size_t j = 0; j < st.length(); j++)
        {
            index2 = st.find(st[j]);
            if(index == index2)
            {
                counter++;
            }
        }
        counters[st[i] - 'a'] = counter; // update 'counters' array
    }
    for(int i = 0; i < 26; ++i)
        if(counters[i] != 0)            // print non-zero counters 
            cout << (char)(i + 'a') << ": " << counters[i] << endl;
    return 0;
}

Output:

e: 1
h: 1
l: 2
o: 1

Character Count in C, #include <stdio.h> int main() { char str[1000], ch; int count = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); printf("Enter a character to find its frequency:​  C Program to count number of lines in a file; C program to copy contents of one file to another file; C program to count number of vowels and consonants in a String; Lex program to count the number of lines, spaces and tabs; C program to delete a file; isspace() in C/C++ and its application to count whitespace characters


I would do something like this:

#include <iostream>
#include <map>
#include <string>

int main()
{
    std::string st;

    std::cout << "Input a sentence: " << std::endl;
    std::getline(std::cin, st);

    std::map<char, int> m;

    for (const char c : st)
        ++m[c];

    for (const std::pair<char, int>& me : m)
        std::cout << me.first << ": " << me.second << std::endl;
}

C Program to Find the Frequency of Characters in a String, For each character read from the file, increment the counter by one. Below is the implementation of the above approach: Program:. C program to count the occurrences of a character in a given string – In this article, we will discuss the various means to count the occurrences of a character in a given string in C programming. Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly.


lechuga2000 beat me to the post, but this is my suggestion:

#include <iostream>
#include <map>
#include <string>

int main()
{
    std::string input_sentence = "Now is the time for all good men to come to the aid of the party.";

/*
    std::cout << "Input a sentence: " << std::endl;
    std::getline(std::cin, input_sentence);
*/

    std::map<char, int> character_counts;

    for (const auto character : input_sentence)
        ++character_counts[character];

    for (const auto counted_character : character_counts)
        std::cout << counted_character.first << ": " << counted_character.second << '\n';

    return 0;
}

And here is the output:

 : 15
.: 1
N: 1
a: 3
c: 1
d: 2
e: 6
f: 2
g: 1
h: 3
i: 3
l: 2
m: 3
n: 1
o: 8
p: 1
r: 2
s: 1
t: 7
w: 1
y: 1

C Program to count the Number of Characters in a File , Character Count Online is an online tool that lets you easily calculate and count the number of characters, words, sentences and paragraphs in your text. You need to initialize charcount. Other than that, it should work, provided that z is a zero-terminated array of characters and m is an int or similar. I would probably write just z [m] rather than z [m] != 0 (since !0 = true and 0 = false), but both work. There are more efficient ways of doing it (although these days I bet a compiler will


Online Character Count Tool, I have the character count program that works like it should, but I can't figure out code to count the number of non-alphanumeric characters. Have ever tried highlighting your work that you would like to count then press Ctrl+Shift+c and it will count it for you evening adding how many characters with and without space, and number of paragraphs. The Website is good anyway. Appears at the top of a search for "Chinese word count without punctuation" but does not fulfill this function


Character count program, C programming, exercises, solution : Write a program in C to count a number of words and characters in a file. Then, a for loop is used to iterate over characters of the string. In each iteration, if the character in the string is equal to the ch , count is increased by 1. Finally, the frequency stored in the count variable is printed.


Programming in C: Count characters in string, C programming, exercises, solution: Write a program in C to count total number of alphabets, digits and special characters in a string. A string is a one-dimensional character array that is terminated by a null character. Frequency of characters in a string is the number of times they occur in a string. For example − String: Football is a sport The frequency of alphabet o in the above string is 3 A program to find the frequency of a particular alphabet is given as follows. Example