hello world hackerrank solution
hello world hackerrank solution : Objective
Hello world hackerrank solution is the series where we will solve and explain the hackerrank c challenge for you.
This is the first hackerrank c challenge which explain how to use printf and scanf function. This challenges your basic ability to take the use input and display the data to the console. To solve this problem you must know how to read the user input and what are the various ways to read the user input. You must know how to print the user input to the standard console. So lets start and see what is the first challenge.
Task
This challenge requires you to print “Hello, World!” on a single line, and then print the already provided input string to stdout. If you are not familiar with C, you may want to read about the printf() command.
Example
s=”Life is beautiful“
The required output is:
Hello, World!
Life is beautiful
Function Description
Complete the main() function below.
The main() function has the following input:
string s: a string
Prints
*two strings: * “Hello, World!” on one line and the input string on the next line.
Input Format
There is one line of text, s.
Sample Input 0
Welcome to C programming.
Sample Output 0
Hello, World!
Welcome to C programming.
How to start thinking for the solution
Step 1 : Write down the approach for solving the problem.
Step 2 : Think about basic use case first and figure it out how to address the task
Step 3 : Think about how to print and what are all different way to print using printf.
Step 4 : Think about how to read the user input and list down all different way to read with scanf.
Step 5 : Think about varibales declaration, defination and usage.
Hint :
User has given a complete sentence which includes spaces as well. Hence we need to read complete line using scanf.
How to start writing the program
Step 1 : Include all header files.
Step 2 : Start writing main function.
Step 3 : Declare character array of size 100.
Step 4 : Use printf to print the “Hello World” to the terminal
Step 5 : Read the user input from the terminal
Step 6 : Printf the user input.
Step 7 : Everything is done normally, hence return from main.
Solution
hello world hackerank in c solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char s[100];
scanf("%[^\n]%*c", &s);
printf("Hello, World!\n");
printf(s);
return 0;
}
Programming Advice
When you read about printf and scnaf then you must study all the user cases and start using such simple function to your daily practice. How to read complete line till the ‘\n’ with white space is the real challenge. Most of engineers doesn’t know how read the sentence with white space. If you practice such use cases you will going to stand out from the crowd.
If you feel you have learn something new, or in any way this articled helped you then please share this article with your techie friends and feel free to be proud about yourself.
Also if you feel what we are doing is right way then please donate us to help you in better way.