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?

Recommended Answers

All 4 Replies

>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.

fgets() will read and add the ENTER key as a newline if there's space in the buffer given.
People are fond of using this piece of code to get rid of it:

size_t len = strlen(string) -1;
if (string[len] == '\n')
    string[len] = '\0';

[Edit:] Didn't see your last question.
>Doesn't my 'getInputByfgets' method takes the new line characters off?
Because sizeof(input) is the sizeof a pointer which is not the same that the length of the string given. printf("%d", sizeof(input)); simple test will prove it to you.

It might, if you use strlen rather than sizeof

Oh, and there can only ever be one newline.
If there is a newline at all, it will always be at
input[strlen(input)-1]

Though you should check fgets() returned success before doing this.

I've changed strlen(string) to sizeof(string) but I still get the same results

I think you got confused here, try this:

#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[MAX];
	strcpy(temp,++inputstring);
	temp[strlen(temp)]=*(--inputstring);
	temp[strlen(temp)+1]= '\0';
	strcat(temp,"ay");
		return temp;
}

void getInputByfgets(char input[]){
	fgets(input, MAX, stdin);
	
        if(input[strlen(input) - 1] == '\n')
             input[strlen(input) - 1] = '\0';

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.