+ 2
What does the for loop actually doing in this code?and what's the function of memset here?
int a[27] String s; memset(al,0,sizeof(al)); Cin>>s; For(int i=0;i<s.size();i++) {a[s[i]-'a']++;}
1 Réponse
+ 2
Overall this program is calculating the frequency of all the letters in the string using hashing.
Let's go through it line by line:-
1) you are creating an array "a" of size equal to numbers of alphabets in English language
2) in this line:
memset(a,0,sizeof(a)); // You are initialising all the element of array "a" to 0
3) then you are inputting the string
4) in the loop you are traversing the string character by character and finding the frequency of corresponding index of the array.
'a' - 'a' = 0 // first index is holding the frequency of letter 'a'
'b' - 'a' = 1 // second index of the array is holding the frequency of letter "b"