How To Take String Input In C Programming Language

Hello guys! My name is Ankur Saxena! I am a writer, software developer, web developer, blogger, traveler and self-publisher. I self-study and R&D various subjects related to History, Geography, Indian Politics, Information Technology (I.T.), Science and Technology, Computer Programming and Web Development and also do writing work related to it. Some of which I publish for all of you readers through my blogging website ANKUR SAXENA BLOG'S...

In this tutorial, we will learn how to take string input in C programming language.


Program 1:

/*
 * Program: Write a C program that accepts a string from the user and display on the console screen.
 * Date: Monday, 01-03-2021
 * @author: Ankur Saxena
 * Platform: Windows 10 Pro/x64/gcc 8.0/Vim editor
 */

/*
Sample TestCase 1:

Input:

Hello World! I am coming back to the race.

Output:

Hello World! I am coming back to the race.
*/

// program start

#include <stdio.h> // c header file
#include <stdlib.h>

int main () // main function
{
    system ("cls"); // used to clear the console screen
    // in linux or macos, use "clear" inside the "system ()" command.
    
    // declare a variable named "in_str" to hold our input
    char in_str [125];


    // read a full line of input from stdin and save it to the "in_str" variable
    scanf ("%[^\n]"in_str);

    // print the output
    printf ("%s\n"in_str);

    return 0;
}

// program end

// Save this file as "TakeString1.c"
// Compile: $ gcc TakeString1.c -o TakeString1 [hit Enter]
// Execute: $ TakeString1 [hit Enter]

/*
Output:

Hello World! I am coming back to the race.
Hello World! I am coming back to the race.
*/

Program 2: Using "fgets ()" command

/*
 * Program: Write a C program that accepts a string from the user and display on the console screen.
 * Date: Monday, 01-03-2021
 * @author: Ankur Saxena
 * Platform: Windows 10 Pro/x64/gcc 8.0/Vim editor
 */

/*
Sample Testcase 1:

Input:

Hello World! I am coming back to the race.

Output:

Hello World! I am coming back to the race.
*/

// program 2: using "fgets ()" command
// program start

#include <stdio.h> // c header file
#include <stdlib.h>

int main () // main function
{
    char ch [125];

    system ("cls");

    // take string input using "fgets ()" command
    fgets (ch, 125, stdin);

    // print the output
    printf ("%s\n", ch);

    return 0;
}

// program end
// Save this file as "TakeInput2.c"
// Compile: $ gcc TakeInput2.c -o TakeInput2 [hit Enter]
// Execute: $ TakeInput2 [hit Enter]

/*
Output:

Hello World! I am coming back to the race.
Hello World! I am coming back to the race.
*/


So friends, this was my blog related to C programming language. I hope you liked my blog / article, and hope that this blog of mine will be helpful for you. Friends, this blog of mine, if able to help you in any way, it will be a matter of great happiness for me. And friends, ... If you want to see other programs and projects related to C, CPP, Java, Python programming, etc., you can visit them by clicking on the link of my Github Profile given below.


If you liked this blog of mine, please subscribe to my blogs through your email id in the Enter your email address at the top of the page. So that you can get the information of any blog / article published on my blogger website first through email. And please share the link of this blog with more and more people. If you have some suggestions related to this article, you can reveal them at the end of the article through comments.

If I have made any kind of error while writing this blog, then I would like to apologize to all you readers for that. So friends, that's all for today's blog for now. See you soon, with a new blog. Till then stay happy, stay healthy, stay safe and keep studying.


Previous Blog: HOW TO INSTALL ELEMENTARY OS 5.1.7 IN DESKTOP OR LAPTOP COMPUTER (HINDI)