Skip to main content

Command Palette

Search for a command to run...

HackerRank - Basic C Language problem solving

Published
โ€ข3 min read
HackerRank - Basic C Language problem solving
R

๐Ÿง‘๐Ÿปโ€๐Ÿฆฑ Passionate B.Tech Computer Science Student | Full-Stack Web Developer

Hello there! ๐Ÿ‘‹ I'm Roni Bhakta, a dynamic and enthusiastic B.Tech Computer Science student with a flair for full-stack web development. My journey in the tech world has been driven by a relentless curiosity and a love for turning innovative ideas into reality.

๐ŸŒ Tech Stack:

Languages: C, C++, Java, Python, JavaScript Web Development: HTML, CSS, JavaScript, Node.js Databases: MongoDB, MySQL Version Control: Git, GitHub

๐Ÿ’ผ Project Highlights: I have independently conceptualized, designed, and implemented a full-stack web development project that showcases my proficiency in crafting end-to-end solutions. Leveraging my skills in front-end technologies (HTML, CSS, JavaScript), back-end frameworks (Node.js), and databases (MongoDB, MySQL), I created a seamless and responsive user experience.

๐Ÿ› ๏ธ Skills Snapshot:

Front-End Development: HTML, CSS, JavaScript Back-End Development: Node.js Databases: MongoDB, MySQL Version Control: Git, GitHub Languages: C, C++, Java, Python ๐Ÿ“š Education: Currently pursuing a Bachelor's degree in Computer Science, where I've not only mastered the theoretical foundations but also applied them hands-on through real-world projects.

๐ŸŒŸ What Sets Me Apart: I thrive on challenges, love collaborating with diverse teams, and am committed to continuous learning. My journey is not just about code but about creating meaningful solutions that make a positive impact.

๐Ÿ”— Connect with Me: Let's connect! Feel free to reach out for discussions, collaborations, or just a good tech chat. Connect with me on LinkedIn or follow me on GitHub.

Let's code the future together! ๐Ÿ’ปโœจ

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.