C Program to Read String and Print It
Write a C Program that Asks for String and Display it
In this tutorial, we will learn how to read string from the user and displaying in the console using C Program. So let’s write a C program to Read String and Print it.
This problem consists of when the program is run, the user will be asked to input a string from the keyboard. After taking input simply display it into the console.
Algorithm – Read String and Print It
In C programming, you can read the string from the keyboard using several ways. Each method is shown below. Before that let’s make an expressive algorithm of this problem;
- In the main function, declare a character array size of 100.
char str[100]
- Ask the user for string input using
scanf()
orgets()
orfgets()
function. Each function has its own advantage and disadvantage. - After taking input, print the string in the console using
printf()
orputs()
function.
Program – Read String and Print It
#include <stdio.h>
int main() {
char str[100];
printf("Enter desired string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
Sample Input | Sample Output |
---|---|
Enter desired string: Allah | You entered: Allah |
Enter desired string: Hello World | You entered: Hello |
WARNING: Notice that for the above program, the second input was Hello World, but in the output, you will get Hello only. It is because using the scanf()
function, the string is taken the first word only.
Program-2
As the scanf()
function can’t read multiple word-based strings. So to solve this program we can use another function for taking input called gets()
function.
#include <stdio.h>
int main() {
char str[100];
printf("Enter your desired string: ");
gets(str);
printf("You entered: %s\n", str);
return 0;
}
Sample Input | Sample Output |
---|---|
Hello World | Hello World |
I love Allah | I love Allah |
WARNING: The gets() function is removed from C11. It will still work, but you may get some warning while execution.
Program-3
We can also read strings using another function called fgets()
#include <stdio.h>
#define ARR_ZISE 100
int main() {
char str[ARR_ZISE];
printf("Enter desired string: ");
fgets(str, ARR_ZISE, stdin);
printf("%s", str);
return 0;
}
Sample Input | Sample Output |
---|---|
Enter desired string: Knowledge is power. | Knowledge is power. |
NOTE: It is the best reliable way of taking string input in C Programming language.
Program-4
Using a special derivative in the scanf()
function, you can read strings continuously. That means you can read multi-word strings.
#include <stdio.h>
int main() {
char str[20];
printf("Enter a string: ");
scanf("%[^\n]%*c", str);
printf("%s\n", str);
return 0;
}
Sample Input | Sample Output |
---|---|
Enter a string: Man is Mortal! | Man is Mortal! |
- C program to print Integer & Decimal number given by user
- C Program to perform all arithmetic calculation
- C program to ask two numbers and find their sum
- C program to check a number is even, odd or zero
- C program to swap between two numbers
- C program to sum of natural numbers up to N
- C program to calculate the factorial of a number
- C program to make a multiplication table
- C program to calculate GCD of two numbers
- C program to calculate LCM of two numbers
- C program to count number of digits in a number
- C program to reverse a integer number
- C program to find the power of a number
- C program to check whether a number is palindrome or not
- C program to count and print prime numbers up to the N
- C program to check armstrong numbers
- C program to check armstrong numbers between two integers
- C program to find factors of a number
- C program to fined the size of all data types
- C program to print largest number among three numbers
- C program to check a number is positive or negetive
- C program to calculate the factorial of a number
- C program to form any kind of pyramid and stracture
- C program to find all the prime numbers between two numbers
- C program to check whether a number is prime or not
- C program to make a simple calculator
- C program to calculate the perimeter of a rectangle
- C program to calculate the area of a rectangle
- C program to calculate the diameter and area of a circle
- C program to convert kilometer to meter and centimeter
- C program to convert temperature celsius to fahrenheit
- C program to convert temperature fahrenheit to celsius
- C program to ask for days and convert into years, week and days
- C program to calculate the square root of a given number
- C program to find third angle of a triangle where two angles are given
- C program to calculate the area of a triangle
- C program to calculate area of an equilateral triangle
- C program to calculate average and percentage of result
- C program to calculate simple loan interest
- C program to compound loan interest
- C program to display fibonacci series of a number
- C program to swap two numbers without using third variable
- C program to print Hello World without using semicolon
- C program to insert assembly code and run it
- C program to printf Hello world without main() function
- C program to to calculate the multiplication of two matrix
- C program to convert decimal numbers into binary
- C program to form a triangle made with alphabet
- C program to form a triangle made with numbers
- C program to form a fibonacci triangle
- C program to convert a number into charecters