Program 83:
Output:
#include<stdio.h> #include<string.h> main() { int i,k=0,count[26]={0},x; char str1[100]; printf("Enter a sentence\n"); gets(str1); while(str1[k]!='\0') { if(str1[k]>='a'&&str1[k]<='z') { x=str1[k]-'a'; count[x]++; } if(str1[k]>='A'&&str1[k]<='Z') { x=str1[k]-'A'; count[x]++; } k++; } for(i=0;i<26;i++) { if(count[i]!=0) { printf("%c occured %d times\n",i+'a',count[i]); } } }
Program to print Frequency of digits in an ArrayExplanation:
- 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
printf("Enter a sentence\n"); gets(str1);
Taking string from the user.-
while(str1[k]!='\0') { if(str1[k]>='a'&&str1[k]<='z')//1st if part { x=str1[k]-'a'; count[x]++; } if(str1[k]>='A'&&str1[k]<='Z')//second if part { x=str1[k]-'A'; count[x]++; } k++; }
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.(refer ascii sheet)
- 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
- Iteration 1:k=0,str1[0]=H which is not '\0'(i.e. not end of string) so the loop is executed
for(i=0;i<26;i++) { if(count[i]!=0) { printf("%c occured %d times\n",i+'a',count[i]); } }
count array whose values are not zero is printed by comparing one by one from 0 to 25- For example count[7]=1 then
"%c occured %d times\n",i+'a',count[i]
i+'a' means 7+'a'=7+97=104 whose value in ascii is 'h'.So h occured count[7] times.Which means h occured 1 times. In this way it will check from count[0] to count[25] and if they are not zero then that value is printed
Output: