I'm making a function that takes a string as input and stores the occurrence of letters from a-z in a map called map1. I have no idea why the output is going beyond 122 ( int('z') ). I also tried while loop, just in case. Here is the code
#include <bits/stdc++.h>
using namespace std;
void makeAnagram(string a) {
int map1[122][2];
for(int i='a';i<='z';i++)
map1[i][1] = 0;
for(int i=0;i<a.size();i++)
map1[a[i]][1]++;
for(int i='a';i<='z';i++)
{
cout<<char(i)<<": "<<map1[i][1]<<endl;
}
}
int main()
{
string s ="abcwedddf";
makeAnagram(s);
return 0;
}
The output is
a: 1
b: 1
c: 1
d: 3
e: 1
f: 1
g: 0
h: 0
i: 0
j: 0
k: 0
l: 0
m: 0
n: 0
o: 0
p: 0
q: 0
r: 0
s: 0
t: 0
u: 0
v: 0
w: 1
x: 0
y: 0
z: 0
{: 32766
|: 32766
}: 21938
~: 32766
: 0
<and continued>.....
Runtime error: SIGSEGV