How do i count characters in char data type?

Aug 11, 2017 at 12:25am
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 :).
Aug 11, 2017 at 1:59am
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";
     }
}
Aug 11, 2017 at 3:21am
Hi,

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

http://en.cppreference.com/w/cpp/algorithm/count
Aug 11, 2017 at 3:38am
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.
Aug 11, 2017 at 7:44am
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.
Aug 11, 2017 at 9:20am
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?
Aug 11, 2017 at 10:02am
@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.
Aug 11, 2017 at 10:55am
Yes he did answer my question I was just explaining my curiosity.
Thank you again
Aug 11, 2017 at 11:13pm
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!
Aug 12, 2017 at 6:44pm
Again thanks mate!
You have helped much
Aug 13, 2017 at 3:43am
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
Aug 13, 2017 at 5:16pm
LOL, I'm guilty of the same multiple times... :O)
Topic archived. No new replies allowed.