Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:39:14+00:00 2026-06-15T15:39:14+00:00

I want to print two strings that was an input from the user using

  • 0

I want to print two strings that was an input from the user using fgets(). fgets() allowed me to store strings with spaces and terminating it with an enter. Following is the example code:

fgets(tTemp ->string, 51, stdin); fflush(stdin);

Now to print it out to the screen:

printf("%s", temp->string); printf(":%s", temp->string2); 
//assuming there are 2 strings

I now want them to print on the same line with the format like this:

string:string1

however the result from the following codes were:

string

:string

how can I make the \n from fgets to not show when I print to get the format I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:39:15+00:00Added an answer on June 15, 2026 at 3:39 pm

    You have to strip it away. First get a pointer to the end, then check if it’s a newline and in that case replace it with a NUL byte.

    char *eptr = tTemp->string + strlen(tTemp->string) - 1;
    
    if (eptr >= tTemp->string && *eptr == '\n')
        *eptr = '\0';
    

    The eptr >= tTemp->string is needed because you might’ve gotten empty string back from fgets in which case eptr would point before the start.

    You also need to check if fgets returns NULL, it’s not clear if you’re doing that or not. If NULL is returned the string buffer is unchanged, whatever was there before will still be there.

    Edit: Actually, reading the man page it seems empty string can’t be returned. At least one character is read if NULL isn’t returned. Still doesn’t hurt to have the check though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context: .Net, C# I want to print a complex number made from two doubles.
I have a program that load data from a file using std::ifstream and store
I want these two print functions to do the same thing: unsigned int Arraye[]
I want to print all the prime numbers between two numbers. This is my
I want to print applet. There are two ways: Signing applet Add permission java.lang.RuntimePermission
I want to do something simple in assembly language. addition two numbers, and print
I want to print an integer number in binary mode using following function: #include
I am getting input from two files on my hard drive: studentNames.txt and studentScores.txt
My class assignment is to write a program that has the user input a
I'm trying to read input from the terminal. For this, I'm using a BufferedReader.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.