count the occurrence of letters in the string

this code is printing strange numbers instead of the number of characters in the input string

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
30
31
32
33
34
35
36
37
38
39
40
41
  #include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
    int counts[26]; int size=0, k;
    string s;
    cout <<"Enter a string " ;
    getline(cin,s);
    for(int i=0; i<s.length(); i++)
    {
            s[i]= tolower(s[i]);
    }   
    for(int i=0; i<26; i++)
    {
    
            for(int j=0; j<s.length(); j++)
            {
                    if(static_cast<char>(i+ 'a') == s[j])
                    {
                         k++;
                    }
            }
            if(k != 0)
            {
                      counts[size]=k;
                      size++;
            }
     }
     for(int i=0; i<s.length(); i++)
     {
           cout << s[i] <<" : " << counts[i]<<endl;
     }
system("pause");
}


just set k=0 before doing any calculations. Since the variable is not initialized, it contains an undefined value.
int counts[26]; int size=0, k=0; //will work
Last edited on
ok, now my code looks like this, but still strange numbers:

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
int counts[26]; int size=0, k;
string s,m;
cout <<"Enter a string " ;
getline(cin,s);
for(int i=0; i<s.length(); i++)
{
s[i]= tolower(s[i]);
}
for(int i=0; i<26; i++)
{
int k=0;
for(int j=0; j<s.length(); j++)
{
if(static_cast<char>(i+ 'a') == s[j])
{
k++;
}
}
if(k != 0)
{
counts[size]=k;
size++;
}
}
for(int i=0; i<s.length(); i++)
{
cout << static_cast<char>(s[i])<< " : " <<counts[i]<<endl;
}
system("pause");
}


the output that i am getting is:

Enter a string welcome to hell
w : 1
e : 3
l : 1
c : 3
o : 1
m : 2
e : 1
: 1
t : 1507312750
o : -2
: 1995550201
h : 1995550275
e : 2686672
l : 2
l : 2686916
Press any key to continue . . .
You're doing a couple things wrong:

First, get rid of the variable size which serves no purpose in your program other than to make you think you're doing something you aren't.

Second, in the final loop s[i] has no relation to counts[i].

Third, you only handle characters in the alphabet correctly and your input includes spaces which are not handled correctly.

You should consider breaking your code up into functions.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// http://ideone.com/tkF9oH
#include<iostream>
#include<string>
#include<cctype>

std::string to_lower(std::string s)
{
    std::string result;
    for (auto ch : s)
        result += std::tolower(ch);
    return result;
}

std::string clean(std::string s)
{
    std::string result;
    for (auto ch : s)
        if (isalpha(ch))
            result += ch;

    return result;
}

std::string process(std::string s)
{
    return to_lower(clean(s));
}

unsigned as_index(char ch)
{
    return ch - 'a';
}

std::string get_line(std::string prompt)
{
    std::string line;

    std::cout << prompt;
    std::getline(std::cin, line);
 
    return line;
}

int main()
{
    const unsigned counts_size = 26;
    int counts[counts_size] = {};    // initialize all elements to 0.

    std::string raw_text = get_line("Enter a string:\n> ");
    std::string text = process(raw_text);

    for (unsigned i = 0; i < text.length(); ++i)
        ++counts[ as_index(text[i]) ];

    for (unsigned i = 0; i < text.length(); ++i)
        std::cout << text[i] << ": " << counts[ as_index(text[i]) ] << '\n';
}


Last edited on
Thanks cire! i was able to figure this out
Topic archived. No new replies allowed.