Playing With Characters in C - Hacker Rank Solution
Hello, Coders today we will be solving Playing with Characters in C Hackerrank Problem.
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 ch 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 ch.
You can take a string as input in C using scanf("%s", s). But it accepts strings only until it finds the first space.
In order to take a line as input, you can use scanf("%[*\n]%*c", s); where s is defined as char s[MAX_LEN] where MAX_LEN is the maximum size of s. 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 lead 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, ch, in the first line. Then print s in the next line. In the last line print the sentence, sen.
Input Format
First, take a character, ch as input.
Then take a string, s as input.
Lastly, take the sentence sen as input.
Constraints
Strings for s and sen will have fewer than 100 characters, including the newline.
Output Format
Print three lines of output. The first line prints the character, ch.
The second line prints the string, s.
The third line line prints the sentence, sen.
Sample Input
C Language Welcome to C!!
Sample Output
C Language Welcome to C!!
Playing With Characters in C Hacker Rank Solution
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ char ch; char s[20]; char sen[100]; scanf("%c", &ch); scanf("%s", &s); scanf("\n"); scanf("%[^\n]%*c", &sen); printf("%c\n", ch); printf("%s\n", s); printf("%s\n", sen); return 0; }
Playing With Characters in C |
Disclaimer: The above problem(Playing With Characters in C) is generated by HackerRank but the solution is given by Sloth Coders. If you have any doubt regarding the Playing With Characters Hackerrank Solution, free feel to contact in the Comment Section.
Happy Coding!!
A Sloth Who loves to Code.