-2
    #include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){

    char noun[25];
    char plural[28];

    fgets(noun,24,stdin);
    strcpy(plural,noun);
    int len=strlen(plural);

    if(plural[len-2]=='h'||plural[len-2]=='s'){
        plural[len-1]='e';
        plural[len]='s';
        plural[len+1]='\0';
    }else if(plural[len-2]=='y'){
        plural[len-2]='i';
        plural[len-1]='e';
        plural[len]='s';
        plural[len+1]='\0';
    }else{
        plural[len-1]='s';
        plural[len]='\0';
    }

    printf("The plural of noun %s is %s\n",noun,plural);
    return 0;
}

the output is always this:

"The plural of noun horse

is horses"

even though i do not put \n the middle of the printf, only in the end.

Andrew
  • 13
  • 3

1 Answers1

0

fgets is picking up a trailing newline character from stdin when you hit enter. Hence noun ends with a newline char

Philip
  • 1,526
  • 1
  • 14
  • 25
  • thanks you, i did what Olaf said and found that out, any help on how to limit input to 25 characters in scanf. is it this scanf("%25s",noun) – Andrew Oct 21 '16 at 02:08