TeachingBee

Playing With Characters HackerRank Solution [Easy]

Problem 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:

  1. Reads a single character.
  2. Reads a word (a string without spaces).
  3. Reads a sentence (a string that may include spaces).
  4. 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:

  1. 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.
  2. Input Operations:
    • scanf("%c", &ch); reads a single character from the input and stores it in the variable ch.
    • scanf("%s", s); reads a word from the input and stores it in the array s. 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 previous scanf. This is necessary because the next input operation uses scanf("%[^\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] tells scanf 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.
  3. Output Operations:
    • printf("%c\n", ch); prints the single character stored in ch.
    • printf("%s\n", s); prints the word stored in the array s.
    • printf("%s\n", t); prints the sentence stored in the array t.

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.

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Hello World! in C HackerRank Solution 

Problem Statement 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

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In