Table of Contents
ToggleProblem Statement
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 stdoutchar 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, ch , in the first line. Then print s in next line. In the last line print the sentence, sen.
Constraints:
- Strings for s and sen will have fewer than 100 characters, including the newline.
Input Format
- First, take a character, ch as input.
- Then take the string, s as input.
- Lastly, take the sentence sen as input.
Example 1:
C
Language
Welcome To C!!
Output
C
Language
Welcome To C!!
Playing With Characters HackerRank Solution Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char ch;
char s[24];
char t[100];
scanf("%c", &ch);
scanf("%s", s);
getchar();
scanf("%[^\n]%*c", t);
printf("%c\n", ch);
printf("%s\n", s);
printf("%s\n", t);
return 0;
}
Explanation of Approach
The program performs the following tasks:
- Reads a single character.
- Reads a word (a string without spaces).
- Reads a sentence (a string that may include spaces).
- Outputs the read character, word, and sentence each on a new line.
Let’s break down the code step-by-step to understand its functionality:
- Variable Declaration:
char ch;
declares a character variable to store a single character.char s[24];
declares a character array (string) to store a word. The size of 24 indicates it can hold up to 23 characters plus a null terminator\0
.char t[100];
declares a larger character array to store a sentence, capable of holding up to 99 characters plus a null terminator.
- Input Operations:
scanf("%c", &ch);
reads a single character from the input and stores it in the variablech
.scanf("%s", s);
reads a word from the input and stores it in the arrays
. The%s
format specifier automatically stops reading at the first whitespace encountered.getchar();
is used to consume the newline character left in the input buffer by the previousscanf
. This is necessary because the next input operation usesscanf("%[^\n]%*c", t);
, which reads until a newline character but doesn’t consume it.scanf("%[^\n]%*c", t);
reads a line of text until a newline character is encountered. The%[^\n]
tellsscanf
to read all characters until a newline is encountered. The%*c
reads and discards the newline character itself, preventing it from being consumed by subsequent input operations.
- Output Operations:
printf("%c\n", ch);
prints the single character stored inch
.printf("%s\n", s);
prints the word stored in the arrays
.printf("%s\n", t);
prints the sentence stored in the arrayt
.
Read Also
Dynamic Array in c Hackerrank Solution
Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.