HackerRank Basic C Questions - 2: Playing with Characters

๐ง๐ปโ๐ฆฑ 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! ๐ปโจ
Lets solve new HackerRank Question on Basic C
Question we will be exploring with some objective is defined below
Difficulty : Easy
Objective
This challenge will help you to learn how to take a character, a string and a sentence as input in C.
To take a single character as input, you can use scanf("%c", &ch );and printf("%c", ch) writes a character specified by the argument char to stdout
char ch;
scanf("%c", &ch);
printf("%c", ch);
This piece of code prints the character .
You can take a string as input in C using scanf(โ%sโ, s). But, it accepts string only until it finds the first space.
In order to take a line as input, you can use scanf("%[^\n]%*c", s);where is defined as char s[MAX_LEN] where is the maximum size of . Here, [] is the scanset character. ^\n stands for taking input until a newline isn't encountered. Then, with this %*c, it reads the newline character and here, the used * indicates that this newline character is discarded.
Note: The statement: scanf("%[^\n]%*c", s); will not work because the last statement will read a newline character, \n, from the previous line. This can be handled in a variety of ways. One way is to use scanf("\n");before the last statement.
Task
You have to print the character, , in the first line. Then print in next line. In the last line print the sentence, .
Input Format
First, take a character, as input.
Then take the string, as input.
Lastly, take the sentence as input.
Constraints
Strings for and will have fewer than 100 characters, including the newline.
Output Format
Print three lines of output. The first line prints the character, .
The second line prints the string, .
The third line prints the sentence, .
Sample Input 0
C
Language
Welcome To C!!
Sample Output 0
C
Language
Welcome To C!!
SOLUTION :
Code ~
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char ch[100],s[50],sen[60];
fgets(ch, sizeof(ch),stdin);
fputs(ch, stdout);
fgets(s, sizeof(s),stdin);
fputs(s, stdout);
fgets(sen, sizeof(sen),stdin);
fputs(sen, stdout);
return 0;
}
Sample Test case 0
Input (stdin)
C Language Welcome To C!!
Your Output (stdout)
C Language Welcome To C!!
Expected Output
C
Language
Welcome To C!!

