Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a program to print names (where names can contain spaces also). I am noticing that my code is printing a new line(\n), then it is doing a tab space (\t). But I don't want it to have a new line. How can I prevent it? Also, I tried using scanf. With scanf it is working (means the expected output is coming), so 1 thing is for sure is that it is not a problem of strings but a problem of fgets.

What I have tried:

My code:

#include <stdio.h>

int main()
{
    char name[3][10];
    for (int i = 0; i < 3; i++)
    {
        printf("Enter name %d\n", i + 1);
        fgets(name[i], 20, stdin);
    }
    for (int i = 0; i < 3; i++)
    {
        printf("%s\b\t", name[i]);
    }
    

    return 0;
}


Now the output is like this:

Enter name 1
Anant G
Enter name 2
Manas K
Enter name 3
Nilesh K S 
Anant G
        Manas K
        Nilesh K S


But I want the output to be as follows
Enter name 1
Anant G
Enter name 2
Manas K
Enter name 3
Nilesh K S 
Anant G        Manas K        Nilesh K S
Posted
Updated 9-Nov-21 5:57am
Comments
Peter_in_2780 9-Nov-21 1:30am    
fgets() is defined to include the trailing newline, so just strip the last character you get.
Member 15423535 9-Nov-21 1:32am    
How can I do it? Can you please explain? I will be grateful!
Peter_in_2780 9-Nov-21 1:50am    
You need to read up on C string handling and string functions. There are several ways you could do it.
Shao Voon Wong 9-Nov-21 2:18am    
int len = strlen(name[i]);
if (len > 0 && name[i][len - 1] == '\n')
name[i][len - 1] = '\0';
Member 15423535 9-Nov-21 2:23am    
Thanks this is working! I added this in my first for loop!

YOu need to remove the trailing newline:
Get the length of the string (strlen[^] will do it).
Make sure it it at least one.
Get the last character of the string: index the length minus one as C arrays use zero-based indexing.
If it's a newline '\n', replace it with a null character '\0'.

I'd write a function which accepted a char* parameter and returned a char* to do it as it's more flexible. I'd probably make it remove trailing space and tab characters as well, and likely make it work with both ends of the string ... and call it Trim
 
Share this answer
 
Comments
CPallini 9-Nov-21 3:20am    
5.
If you are using Linux you should probably use getline()[^] instead of fgets(). getline() reads and allocates a buffer, and returns the number of characters read. This means that there's no need to try to calculate the length of the buffer, and/or test the last character in the buffer to determine if its a newline or not
C
/* example reading a file using getline() */
/* assume we already have a FILE *input already open for reading */

#include <stdio.h>
#include <stdlib.h>

char *buff = NULL;   /* input buffer */
size_t bufflen = 0;  /* current size of input buffer */
ssize_t rlen;        /* number of bytes read from input */

while( (rlen = getline(&buff, &bufflen, input) > 0 ) {
   buff[rlen] = '\0';
   /* process input buffer */
}
free(buff);         /* dispose of input buffer when we no longer need it */

The bufflen parameter to getline() is the current allocated size of the buffer, which getline uses to reallocate only when necessary. An informal test shows that getline() to be about 30% faster than using fgets() and strlen().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900