How do i count characters in char data type?

Hi I know how to count characters in a string.But I was wondering how do I ouput the number of characters or a specific symbol in it?In char data type nothing related to strings.

I was curious and I couldn't find an answer to my question.
thank you for your time :).
pass the characters of the std::string to a std::map<char, size_t>:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# include <iostream>
# include <string>
# include <map>

int main()
{
     std::string myString = "helloWorld";

     std::map<char, size_t> myMap{};

     for (const auto& elem : myString)
     {
         myMap[elem]++;
     }
     for (const auto& elem : myMap)
     {
         std::cout << elem.first << " " << elem.second << "\n";
     }
}
Hi,

To count a particular char, rather than all of them, try this:

http://en.cppreference.com/w/cpp/algorithm/count
If you are learning how to do this as part of introductory CS, then consider how you would count anything:

1. You need a variable to keep track of the number of times you found something.

2. You need to loop through all things and update your 'count' variable every time you found what you are looking for.

Otherwise std::count() and std::count_if() are the droids you are looking for.
As it says above I was curious how does this work with char data types.
Because I was doing some tasks and I asked myself how do you count with char data type without any string involvement.
I'm pretty sure I just answered that question. Perhaps you mean something other than what I am reading?

Can you provide an example of what you would like to do?
@stonedviper

What do mean by no string involvement?

No std::string, or a C string is an array of char?

Either way Duthomhas has answered your question.
Yes he did answer my question I was just explaining my curiosity.
Thank you again
No need for anyone to get mad at OP.

A std::string or char* are the two most common character string representations, but any sequence can deliver data.

Such as the standard input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cctype>
#include <iostream>
int main()
{
  int nuppers = 0;
  int ntotal = 0;
  char c;
  cout << "Counts uppercase letters. Type away!\n"
          "(Press Ctrl+Z, Enter [Windows] or Ctrl+D [Linux] to finish.\n";
  while (cin.get( ch ))
  {
    ntotal++;
    nupper += isupper( ch );
  }
  cout << "You entered " << nuppers << " out of " << ntotal << " characters.\n";
}

Have fun!
Again thanks mate!
You have helped much
Duthomhas wrote:
No need for anyone to get mad at OP.


Hi,

Apologies, I didn't intend my last post to be interpreted that way :+)

Sometimes things might seem a little terse; and it can be hard to interpret tone / intentions / emotions on the net. My bad for not putting enough smiley faces or other niceties :+).

Anyway best wishes and regards to all :+D
LOL, I'm guilty of the same multiple times... :O)
Topic archived. No new replies allowed.