I haven't gone into much details yet because I am just trying to get this code to work first. The program runs with zero errors but when I run the program the printf statement at the end prints numbers like -858993451 when instead I want to show the number of characters for a, then b, then c. What is the problem? I assume it is something basic with my code and how it increments.
Code:void main() { FILE *inPtr; char alpha[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int numbers[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int letter[ALPHABET]; int digit[NUM]; int ch, i, x, y, z; y = 0; inPtr = fopen("C:\\Temp\\indata.txt", "r"); if(!inPtr) { printf("\nError open input file\n"); exit(13); } while(1) { ch = fgetc(inPtr); if(ch == EOF) break; else if(isalpha(ch)) { for(i = 0; i < ALPHABET; i++) { ch = tolower(ch); if(ch == alpha[i]) letter[i]++; } } else if(isdigit(ch)) { for(x = 0; x < NUM; x++) { if(ch == numbers[x]) digit[x]++; } } else if(ispunct(ch)) y++; } for(z = 0; z < ALPHABET; z++) { printf("%c's: %d\n", alpha[z], letter[z]); } for(z = 0; z < NUM; z++) { printf("\n%d's: %d", numbers[z], digit[z]); } printf("\nSpecial Characters found: %d\n", y); fclose(inPtr); }



2Likes
LinkBack URL
About LinkBacks



