0

When I use "fgets" (in gcc) it saves new line character within the entered string, I want to remove the new line character.

#include <iostream>
#include <cstring>

using namespace std;

int main()
{

char x[10];

fgets(x,10,stdin);
cout << x << endl;

return 0;
}

From comments I went to Removing trailing newline character from fgets() input

and I used "strcspn" and it works fine like:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{


char x[20];

fgets(x,sizeof(x),stdin);

x[strcspn(x,"\n")]=0;

cout << x << endl;

if(!strcmp(x,"hello world!"))
{
cout << "yes" << endl;

}

return 0;
}
pascal111
  • 37
  • 3
  • So, remove it the same way you'd remove any character from any string. I'm not sure what the question is. – IMSoP Oct 03 '21 at 11:06
  • 1
    See https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input – emptyhua Oct 03 '21 at 11:19

0 Answers0