I'm getting an unexpected error for my piece of code. It seems like a new line character is added into the string once is it called.
Well the purpose of my lil code is to read characters from the keyboard using the fgets function and then convert it into pig latin language.
for eg. (with error)
Enter a string: Good
ood
Gay
It should be 'oodGay'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
char * toPLString(char *inputstring);
void getInputByfgets(char input[]);
int main(int argc, char *argv[]) {
char string[MAX];
printf("Enter a string: ");
getInputByfgets(string);
printf("%s", toPLString(string));
}
//Convert to pig latin
char* toPLString(char * inputstring){
static char temp[50];
strcpy(temp,++inputstring);
temp[strlen(temp)]=*(--inputstring);
temp[strlen(temp)+1]= '\0';
strcat(temp,"ay");
return temp;
}
void getInputByfgets(char input[]){
fgets(input, 50, stdin);
int i;
for ( i = 0; i < sizeof(input); i++ )
if ( input[i] == '\n' ) {
input[i] = '\0';
}
}
Doesn't my 'getInputByfgets' method takes the new line characters off?