Printing Tokens | hackerrank practice problem solution

   Problem:-

Given a sentence, , print each word of the sentence in a new line.

Input Format

The first and only line contains a sentence, .

Constraints

Output Format

Print each word of the sentence in a new line.

Sample Input 0

This is C 

Sample Output 0

This
is
C

Explanation 0

In the given string, there are three words [“This”, “is”, “C”]. We have to print each of these words in a new line.

Sample Input 1

Learning C is fun

Sample Output 1

Learning
C
is
fun

Sample Input 2

How is that

Sample Output 2

How
is
that

Solution:-

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

char s[1000];
scanf(“%[^n]”, s);
for(int i=0;s[i];i++)
{
if(s[i]==‘ ‘)
printf(“n”);
else {
printf(“%c”,s[i]);
}
}
return 0;
}

Recommended post:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

Key points:-


Leave a Reply

Your email address will not be published.