|
This is the handout from class, 5/6/97. It illustrates the use of an array to keep track of the number of times each letter of the alphabet is used in a message. Note that the array of integers should be initialized to be all zeroes before processing the text of the message.
#include <stdio.h>
int main(void)
{
int letters[26];
int counter;
char ch;
for (counter=0;counter<26;counter++)
letters[counter]=0;
printf("Enter characters; enter a # to quit.\n");
while ((ch=getchar()) != '#')
{ if (ch >= 'a' && ch <= 'z')
letters[ch-97]++;
else if (ch >= 'A' && ch <= 'Z')
letters[ch-65]++;
}
printf("Output: \n");
for (counter=0;counter<26;counter++)
{ if (counter % 5 == 0)
printf("\n");
printf("%c = %d ",counter+65,letters[counter]);
}
}
|