 ### Program 83:To know the frequency of each character in sentence

Program 83:
```#include<stdio.h>
#include<string.h>
main()
{
int i,k=0,count={0},x;
char str1;
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 Array`
Explanation:
1. This program starts with initializing :
• str1 → To store string with length of 100 which means it can store 100 letters
• i ,k,x→Used as helping variable
• count→ To count number of times each letter is repeated from a-z
2. ``` printf("Enter a sentence\n");
gets(str1);```
Taking string from the user.
3.
```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 means each one from count to count are initialized to zero.
• Iteration 1:k=0,str1=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-'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++→count=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=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-'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++→count=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=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-'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++→count=1.
• now k++ so k is incemented by 1,then k=3.
• Iteration 4:k=3,str1=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-'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++→count=1+1=2.
• now k++ so k is incemented by 1,then k=4.
• Iteration 5:k=4,str1=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-'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++→count=1.
• now k++ so k is incemented by 1,then k=5.
• Iteration 6:k=5,str1is the end of string '\0'(i.e. not end of string) so the loop is terminated.
• Finally values are
• count, count.....count=0
•  count=1
•  count...count=0
•  count=1
•  count....count=0
• count=2
• count..count=0
•  count=1
•  count..count=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=4 as in (hHhH) where h-2 times and H-2 times so a total of 4 times
4. ``` 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
5. For example count=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 times.Which means h occured 1 times. In this way it will check from count to count and if they are not zero then that value is printed

Output: 