There are 3 logical errors in the code,
1) Variables QL, QD, QP, QS, QO are to be initialized to 0, since you are using it in the later part of the program.
2) I don't understand the condition in the for-loop
Code:
for(P=0; S[P] < S[SIZE - 1]; P++)
If you want to traverse through the entire string then the for-loop would be,
Code:
for(P=0; P < SL - 1 ; P++)
3) Instead of,
Code:
while(S[P] != '\0')
it has to be,
-=-=-=-=-=-=-=-
so the rewritten code will be
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define SIZE 80
int main(void)
{
char S[SIZE];
int SL;
int P;
int QL=0;
int QD=0;
int QP=0;
int QS=0;
int QO=0;
system("cls");
printf("CHARACTER COUNTER\n\n");
printf("Written by Ross Buffington == 12/03/2003\n\n");
printf("This program will request and store a string of characters\n");
printf("and then report how many of the selected characters are:\n");
printf(" Letters\n");
printf(" Numerals\n");
printf(" Punctuation\n");
printf(" Spaces\n");
printf(" or Other\n\n");
fflush(stdin); /* Flush Keyboard Buffer */
printf("On the line below, enter the string that should be analyzed:\n");
printf(">");
fgets(S, SIZE, stdin);
if(S[strlen(S) - 1] == '\n')
S[strlen(S) - 1] = '\0';
SL = strlen(S);
for(P=0; P < SL-1; P++)
if(S[P] != '\0')
{
if(isalpha(S[P]))
QL = QL + 1;
else if(isdigit(S[P]))
QD = QD + 1;
else if(ispunct(S[P]))
QP = QP + 1;
else if(isspace(S[P]))
QS = QS + 1;
else
QO = QO + 1;
}
printf("\nANALYSIS RESULTS:\n\n");
printf("String Length: %d\n", SL);
printf("Letters: %d\n", QL);
printf("Numerals: %d\n", QD);
printf("Punctuation: %d\n", QP);
printf("Spaces: %d\n", QS);
printf("Other: %d\n", QO);
fflush(stdin); getchar(); /*Pauses the program before exit*/
return(0);
}