C Program to Check Frequency of Characters
Program:
Output:
Explanation (How it Works):
This program starts with initializing :
str1[100] → To store string with length of 100 which means it can store 100 letters.
i ,k,x→Used as helping variable.
count[26]→ To count number of times each letter is repeated from a-z.
str1[100] → To store string with length of 100 which means it can store 100 letters.
i ,k,x→Used as helping variable.
count[26]→ To count number of times each letter is repeated from m a-a-z.
Displays Output
Taking string from the user.
While Loop
Lets take a small example
str1="Hello".count[26] means each one from count[0] to count[26] are initialized to zero.
Iteration 1: k=0,str1[0]=H which is not '\0'(i.e. not end of string) so the loop is executed.
- 'H' lies between 'A' and 'Z' so the second if part is executed.
- x=str1[k]-'A' →str1[0]-'A'→'H'-'A'→x=72-65=7.
- 'H'-'A' will give integer value as output as the output of the result is stored in integer variable.
- Where 'H' ascii value is 72 and that of 'A' is 65 so 72-65 is 7.
- Therefore,x=7.
- count[7]++→count[7]=1 as it is previously initilized to 0.
- Now k++ so k is incemented by 1,then k=1.To move to the next character/letter.
Iteration 2: k=1,str1[1]=e which is not '\0'(i.e. not end of string) so the loop is executed.
- 'e' lies between 'a' and 'z' so the first if part is executed.
- x=str1[k]-'a' →str1[1]-'a'→'e'-'a'→x=101-97=4.
- 'e'-'a' ('e' ascii value is 101 and that of 'a' is 97 so 101-97 is 4).
- Therefore, x=4.
- count[4]++→count[4]=1 as it is previously initilized to 0.
- Now k++ so k is incemented by 1,then k=2.To move to the next character/letter.
Iteration 3: k=2,str1[2]=l which is not '\0'(i.e. not end of string) so the loop is executed.
- 'l' lies between 'a' and 'z' so the first if part is executed.
- x=str1[k]-'a' →str1[2]-'a'→'l'-'a'→x=108-97=11.
- 'l'-'a' ('l' ascii value is 108 and that of 'a' is 97 so 108-97 is 11).
- Therefore, x=11.
- count[11]++→count[11]=1.
- Now k++ so k is incemented by 1,then k=3.
Iteration 4: k=3,str1[3]=l (again) which is not '\0'(i.e. not end of string) so the loop is executed.
- 'l' lies between 'a' and 'z' so the first if part is executed.
- x=str1[k]-'a' →str1[3]-'a'→'l'-'a'→x=108-97=11.
- 'l'-'a' ('l' ascii value is 108 and that of 'a' is 97 so 108-97 is 11)
- Therefore, x=11.
- count[11]++→count[11]=1+1=2.
- Now k++ so k is incemented by 1,then k=4.
Iteration 5: k=4,str1[4]=o which is not '\0'(i.e. not end of string) so the loop is executed.
- 'o' lies between 'a' and 'z' so the first if part is executed.
- x=str1[k]-'a' →str1[4]-'a'→'o'-'a'→x=111-97=14.
- 'o'-'a' ('o' ascii value is 111 and that of 'a' is 97 so 111-97 is 14).
- Therefore, x=14.
- count[14]++→count[14]=1.
- Now k++ so k is incemented by 1,then k=5.
Iteration 6: k=5,str1[5]is the end of string '\0'(i.e. not end of string) so the loop is terminated.
Finally values are,
- count[0], count[1].....count[3]=0
- count[4]=1
- count[5]...count[6]=0
- count[7]=1
- count[8]....count[10]=0
- count[11]=2
- count[12]..count[13]=0
- count[14]=1
- count[15]..count[25]=0
Remember either the letter is 'H' or 'h' both their count is recorded in same array i.e if 'h' or 'H' both their total count occurs 4 times then count[7]=4 as in (hHhH) where h-2 times and H-2 times so a total of 4 times.
Post a Comment