HackerRank - Basic C Language problem solving

HackerRank - Basic C Language problem solving

Code difficulty level: EASY

Let's start with understanding the problem what is the objective and what prerequisite knowledge we should have to solve the below problem. we will go through the knowledge we need to know to solve the following problem.

Objective

In this challenge, we will learn some basic concepts of C that will get you started with the language. You will need to use the same syntax to read input and write output in many C challenges. As you work through these problems, review the code stubs to learn about reading from stdin and writing to stdout.

Task

This challenge requires you to print 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

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.

Sample Input 0

Welcome to C programming.

Sample Output 0

Hello, World!
Welcome to C programming.

Let's start with prerequisite knowledge as we know, In C programming, stdin and stdout are standard input and standard output streams, respectively.

stdin refers to the standard input stream. It's typically used to read input from the user or from another program. In C, you can use functions like scanf() or fgets() to read input from stdin.

stout refers to the standard output stream. It's used to write output to the console or another destination. Functions like printf() or puts() are used to write output to stdout.

These streams are part of the standard I/O (input/output) library in C and are usually associated with the keyboard for input (stdin) and the screen/console for output (stdout).

so for the desired out put we need to use fputs() & fgets()

• fputs() is nothing but printf().

•fgets() is nothing but scanf().

To use fputs() we need to understand the syntax of fputs()

fputs(const char restrict s, FILE restrict stream);

To use fgets() we need to understand the syntax of fgets()

fgets(char restrict s, int n, FILE restrict __stream);

Solution to the problem is :

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

int main() 
{

    char s[100];
    fgets(s, sizeof(s), stdin);
    fputs("Hello, World!\n", stdout);
    fputs(s, stdout);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Autotest :**

Sample Test Case 0**

Input (stdin)

  •     Welcome to C programming.
    

Your Output (stdout)

  •     Hello, World!
    
  •     Welcome to C programming.
    

Expected Output

  •     Hello, World!
        Welcome to C programming.