+ 1
How can I stop the enter sequence being taken as a character?
https://code.sololearn.com/cnwns6XFKsuS/?ref=app Here if you give two string inputs, the first output gets printed like "This is first input " And second like " This is second" Notice the second double quote in the first line gets printed in the next line instead of the first line itself. How can I fix this? And why does the length of the first input gets printed as +1 of the value, eg if "hai" instead of 3 o/p is 4? How?
4 Respostas
+ 2
fgets() stores any newline character as part of the string, so you have to manually remove them:
a[strcspn(a, "\n")] = '\0';
b[strcspn(b, "\n")] = '\0';
+ 2
strcspn() scans the first string for any character in the second string ("\n" here).
If found, it returns the position of that character in the string. If not found, it returns the last positon in the string (which should be '\0').
We use the return value of strcspn() as an index into the character array (a and b) and assign a NUL-terminating byte where the newline character is located. If there is no newline character, it'll assign a '\0' at the end of the string (i.e it has no effect).
+ 1
Komodo64 Oh thanks this works perfectly.
But can you please explain what these lines actually do?
0
Komodo64 thanks mate, now I get it!