Getting & Correcting String Input with fgets
The command line input funciton fgets typically pulls extra line feed (ascii 10) and carriage return (ascii 13) characters, and will also allow for trailing white-space (space = ascii 32 and tab = ascii 9).
The below source demonstrates some different methods for correcting for some of these extra inputs.
stringInputCorrection.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char userIn[100]; // declare a char array to hold string up to 100
int userInLength;
// User inputs text. Notice that there is an extra character or two
// a new line '\n' = 10 and/or a carriage return '\r' = 13
puts("Enter some text:");
fgets(userIn, 100, stdin);
puts("Echo. You input the string:");
puts(userIn);
userInLength = strlen(userIn);
printf("Your text was %d characters long.\n", userInLength);
puts("Here are the ascii codes in the array:");
for (int i = 0; i < userInLength + 1; i++){
printf("userIn[%d] = %d\n", i, userIn[i]);
}
//Meth.1: Correcting by over-writing with 0 by length subtraction
char userInAgain[100];
int inLengthAgain;
puts("Test 2. Enter some text:");
fgets(userInAgain, 100, stdin);
//Correct for line return by trimming string length by 1 or 2.
inLengthAgain = strlen(userInAgain);
userInAgain[inLengthAgain - 1] = 0;
//analyze - recalc. length
puts("Echo. You input the string:");
puts(userInAgain);
inLengthAgain = strlen(userInAgain);
printf("Your text was %d characters long.\n", inLengthAgain);
puts("Here are the ascii codes in the array:");
for (int i = 0; i < inLengthAgain + 1; i++){
printf("userInAgain[%d] = %d\n", i, userInAgain[i]);
}
//Meth.2: Correcting by over-writing with 0 by searching for a line return
char userInYetAgain[100];
int inLengthYetAgain;
puts("Test 3. Enter some text:");
fgets(userInYetAgain, 100, stdin);
//line return correction - search first then trim
char* nlPointer = strchr(userInYetAgain, '\n'); // locate pointer to new line if it exists.
if (nlPointer != NULL) *nlPointer = 0; // trim string back to line return if it is found.
char* crPointer = strchr(userInYetAgain, '\n'); // locate pointer to carriage return if it exists.
if (crPointer != NULL) *crPointer = 0; // trim string back to carriage return if it is found.
//output string data
puts("Echo. You input the string:");
puts(userInYetAgain);
inLengthYetAgain = strlen(userInYetAgain);
printf("Your text was %d characters long.\n", inLengthYetAgain);
puts("Here are the ascii codes in the array:");
for (int i = 0; i < inLengthYetAgain + 1; i++){
printf("userInAgain[%d] = %d\n", i, userInYetAgain[i]);
}
//Meth.3: Correcting by trimming in while loop - 'proper trim'
char userInLast[100];
int inLengthLast;
puts("Test 4. Enter some text:");
fgets(userInLast, 100, stdin);
// todo correct with loop.
inLengthLast = strlen(userInLast);
char lastChar = userInLast[inLengthLast - 1];
while (lastChar == '\n' || lastChar == '\r' || lastChar == ' ' || lastChar == '\t'){
userInLast[inLengthLast - 1] = 0;
inLengthLast = strlen(userInLast);
lastChar = userInLast[inLengthLast - 1];
}
//output string data
puts("Echo. You input the string:");
puts(userInLast);
inLengthLast = strlen(userInLast);
printf("Your text was %d characters long.\n", inLengthLast);
puts("Here are the ascii codes in the array:");
for (int i = 0; i < inLengthLast + 1; i++){
printf("userInLast[%d] = %d\n", i, userInLast[i]);
}
return 0;
}