In this tutorial, we will solve HackerRank Playing with characters problem in c or write a solution for this problem. In this problem, we need to take a character as input, a string as input and then we need to take a sentence as an input.
The string and sentence will always be less than 100 characters. on the output screen, we need to print the three lines. first, we need to print the character value then in the second line we need to print the string value, and in the last sentence value.
Using this problem we will learn or understand how we can take a character, string, and even a full sentence as input in the c programming language.
Solution in C programming.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char ch,s[100],sen[122];
scanf("%c\n%s\n%[^\n]",&ch,&s,&sen);
printf("%c\n%s\n%s",ch,s,sen);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Explanation
In the above solution first, we included the necessary header files. after that we defined main() function. in the main function first we defined a character variable ch, an array of characters of length 100, and then again an array of characters with a length of 122.
After that, we use the scanf() function to read all the variable values using the %C for character variable and %s for string and sentence variable. after that, we used the printf() function to print all the values. for the new line, we have used the \n (newline character) to go to the new line.
Second solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
char ch,s[100],sen[122];
scanf("%c", &ch);
scanf("%s", &s);
scanf(" %[^\n]%*c", &sen);
printf("%c\n%s\n%s",ch,s,sen);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Explanation
This second solution is the same as the first solution. the difference is that we have used the three different scanf() functions to scan the values.
Sharing the knowledge
I hope anyone who is reading this solution liked it then please share it for once.