#include /* using printf, fgets, stdin, BUFSIZ */ #include /* using exit */ #include /* using strlen */ int main() { char input[BUFSIZ]; char * prompt = "Please enter a string followed by : "; printf("%s", prompt); if (fgets (input, BUFSIZ, stdin)) /* fgets returns NULL if EOF */ { int i; int stringLength = strlen(input); /* Remove newline if it is there by replacing it with a null byte. */ if ( stringLength > 0 && input[stringLength-1] == '\n' ) { input[stringLength-1] = '\0'; stringLength--; } /* On Windows, there might also be a carriage return; remove it too. */ if ( stringLength > 0 && input[stringLength - 1] == '\r') { input[stringLength-1] = '\0'; stringLength--; } /* If there is no input left, we're done. */ if ( stringLength == 0 ) { exit(0); } /* Show the ASCII character and decimal & octal representations * of each character. */ for ( i = 0; i < stringLength; i++ ) { char c = input[i]; printf("The character '%c'", c); printf(" is represented with the ASCII value %d (decimal)", c); printf(" or %o (octal).\n", c); if ( input[i] >= '0' && input[i] <= '9' ) { int numericValue = input[i] - '0'; printf("\tThe numeric value is %d.\n", numericValue); } } } exit(0); }