Counting characters

Oct 21, 2017 at 3:56am
Im learning how to code and I have this homework where I have to show how many of each characters of a string are in the sttring, I using this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a = 0;
    char letter;
    string mystring;
    cin >> mystring;
    for (int x = 0; x < mystring.length(); x++)
    {
        letter = mystring.at(x);
        switch (letter) {
            case 'a':
                a++;
                break;
            default:
                break;
        }
        if(mystring.at(x) == 'a')
    {
        cout << "a: " << a << endl;
    }
    }
    
    return 0;
}


This is just for a I will do the same for the other letters but when I run it for the string aaa I get: "a: 1 a: 2 a: 3" and I want: "a: 3 a: 3 a: 3" how do I do it, thanks in advance.
Oct 21, 2017 at 4:08am
Why would you want to print "a: 3" three times? Shouldn't it print out once to show how many 'a' are inside the string?
Oct 21, 2017 at 4:14am
I know it seems dumb but the teacher wants it like that I need a code that for example if my string is "lol" it would print "l: 2 o: 1 l: 2"
Oct 21, 2017 at 6:44am
This is my approach to the problem, feel free to use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

}
int main()
{
	
	const char chararray[64] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122};
	int wordCount[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
	
	std::string holdString;
	std::getline(std::cin, holdString);
	for (auto &characterInBuff : holdString)
	{
		for (int index(0); index < 64; index++) if (characterInBuff == chararray[index]) wordCount[index]++;
	}

	for (auto &characterInBuff : holdString)
	{
		for (int index(0); index < 64; index++) if (characterInBuff == chararray[index]) std::cout << chararray[index] << " = " << wordCount[index] << '\n';
	}
	std::cin.get();
	return 0;
}


EDIT:I made a redundant change, just made charArray a constant array.
Also i forgot to mention the problem with your code, COUT is still inside the for loop, thus every time the program loops at line 12 your code will get executed, move line 23 to line 28(outside of the loop) to fix your original method, keep in mind this will not decrease the ammount of code you'll have to write if you want to continue with your original mehtod
Last edited on Oct 21, 2017 at 7:12am
Oct 21, 2017 at 8:17am
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
   string mystring;
   map<char,int> mymap;
   cout << "Enter string: ";   cin >> mystring;
   for ( char c : mystring ) mymap[c]++;
   for ( char c : mystring ) cout << c << ": " << mymap[c] << "  ";
}


Enter string: $$$lol$$$
$: 6  $: 6  $: 6  l: 2  o: 1  l: 2  $: 6  $: 6  $: 6 

Oct 21, 2017 at 8:17am
The approach from @AndreGS is ok, but the use of numeric codes in chararray is unnecessarily mysterious. It might be more legible as:
 
const char chararray[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
(Also remember here that the length is 52, not 64).
Or use a std::string.

To initialise the counts array to zeros, it is sufficient to put int wordCount[64] = { 0 ); or just int wordCount[64] {}; from C++11 onwards.

In the map code from @lastchance above, as an alternative to the last line
 
    for ( char c : mystring ) cout << c << ": " << mymap[c] << "  ";

one might consider this, to avoid duplicated counts in the output
 
    for (const auto& item : mymap) cout << item.first << ": " << item.second << "  ";

Enter string: $$$lol$$$
$: 6  l: 2  o: 1

Last edited on Oct 21, 2017 at 9:52am
Topic archived. No new replies allowed.