Hi
I'm trying to create a program that reads in a block of text from the keyboard, analyze the text and display the output in the format shown below. However i have to use two arrays one is a character holder (each time a new character is found it will be placed in that array. Only one of each character may be placed in the array). the second array is a count array. i am allow to use isalpha function.
character count
H 3
r 2
u 3
. .
. .
This is the code i have so far
Code:
#include "stdafx.h"
#include "stdio.h"
int _tmain(int argc, _TCHAR* argv[])
{
#define ALPHABET_TOTAL 26 /* The total number of letters in the alphabet */
int text[ALPHABET_TOTAL] = {0}, /* Array sized 26, acting as counters for all characters 'a' .. 'z' (case insensitive) */
text_input, /* Reads in the text input as characters */
ASCII_count, /* The ASCII numerical representation of letters in the alphabet */
alpha; /* boolean "flag" which turns to 0 (false) in case of a non-alpha character */
/* Initialization */
text_input = 0;
ASCII_count = 0;
alpha = 0;
/* Get input */
printf("\nPlease enter text. Terminate program using Ctrl+Z: ");
while( text_input != EOF )
{
alpha = 1;
text_input = getchar();
/* If input is alphabetical, convert to a number 0-25 so that it can be used as an array index locator */
if(text_input >= 'a' && text_input <= 'z')
text_input -= 'a';
else if(text_input >= 'A' && text_input <= 'Z')
text_input -= 'A';
/* Non-alphabetical character, set the alpha flag to false */
else
alpha = 0;
/* Increment the appropriate counter in the text array as long as the character is alphabetical */
if(alpha)
text[text_input]++;
}
/* Display results */
for (ASCII_count; ASCII_count <= 25; ASCII_count++)
printf("\nTotal %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A', text[ASCII_count]);
for(;;);
}
The problem is that i don't want to display all the letters in the alphabet only the ones that appear in the text. Also I need to come up with a way to differentiate lower case from upper case.
Any help would be greatly appreciate it.
Thanks