views:

40

answers:

3

I have having some trouble printing a "char" from an ASCII value stored in an array.

The goal overall is to read a string of input from the user, store that in an array. Then use another array to count the number of times each character appears in the string, storing the number of occurances in the array address that matches the ASCII code of the character being counted. Then to output that number of occurances along with which character was counted, and the percentage of characters.

I can successfully make the array of character occurances. And as a test I can do the counting and percentage calculations during the occurance count. But later when I try to make a FOR loop with a IF statement that ignores NULL values and prints the array address as the ASCII value for the character, the number of times the character occured, and the percentage that character appears of the total characters. I just get a NULL character printed. Below you find what I have written so far.

This is for help with a school assignment, and I am NOT asking for the solution in code. I've been trying to find out where I went wrong, and I'm not sure why one of my FOR loops works and the second one doesn't. And would appreciate some help in finding where I've gone wrong.

#include <stdio.h>
#include <stdlib.h>
#define TERMINAL -9
int main()
{

float totalCharacters;
float currentCharacterCount;
int iteration, currentInputCharacter;
char checkCharacter, input [80];
int alphabetArray[128] = {0};

totalCharacters = 0;
iteration = 0;
currentCharacterCount = 0;
currentInputCharacter = 0;
alphabetArray[128] = 0;

printf("Enter a line of test: ");
gets(input);

printf ("FREQUENCY TABLE\n");
printf ("---------------\n");
printf ("Char Count %% of Total\n");
printf ("---- ----- ----------\n");

totalCharacters = strlen(input);

printf (" ALL %5d %9.2f%%\n", strlen(input), (totalCharacters / strlen(input)) * 100);

for ( iteration = 0; input[iteration] != '\0'; iteration++)
{
      checkCharacter = input[iteration];
      alphabetArray[checkCharacter]++;
      printf ("%4c %5d %9.2f%%\n", checkCharacter, alphabetArray[checkCharacter], ((float)alphabetArray[checkCharacter] / strlen(input)) * 100);
}

for ( iteration = 0; alphabetArray[iteration] != '\0'; iteration++)
{
     printf ("%1c ", alphabetArray[iteration]);

    if (alphabetArray[iteration] != '\0')
    {
       checkCharacter = alphabetArray[iteration];

       printf ("%4c %5d %9.2f%%\n", checkCharacter, alphabetArray[checkCharacter], ((float)alphabetArray[checkCharacter] / strlen(input)) * 100);
    }
}


system ("pause");
exit(0);

}
A: 

Try changing this:

for ( iteration = 0; alphabetArray[iteration] != '\0'; iteration++) 
{ 
     printf ("%1c ", alphabetArray[iteration]); 

    if (alphabetArray[iteration] != '\0') 
    { 
       checkCharacter = alphabetArray[iteration]; 

       printf ("%4c %5d %9.2f%%\n", checkCharacter, alphabetArray[checkCharacter], ((float)alphabetArray[checkCharacter] / strlen(input)) * 100); 
    } 
} 

to this (Note: untested, but on the right track):

for ( checkCharacter = 1; checkCharacter < 128; checkCharacter++) 
{ 
    putchar(checkCharacter);     
    if (alphabetArray[iteration] != 0) // If cur char has non-zero occurance count
       printf (" %4c %5d %9.2f%%\n", checkCharacter, alphabetArray[checkCharacter], ((double)alphabetArray[checkCharacter]) / strlen(input) * 100.0); 
    else
       putchar('\n');
} 
Michael Goldshteyn
+1  A: 

The Second for loop iterates over the alphabetArray which is NOT a string so is not terminated by '\0'.

It would be better to use the fact that there are only 26 letter, and have an array 26 long. you can convert letters to position in the alphabet by somechar - 'a' for small letters and somechar - 'A' for capitals, though you would need to check for characters that aren't in the range from 'a' to 'z' and 'A' to 'Z' such as !@£$%^&*().?.

Twig
A: 

alphabetArray is type int, so it wont be null.

Oliver