Hello Programmers, In this post, you will know how to solve the Printing Tokens in C HackerRank Solution. This problem is a part of the HackerRank C Programming Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Printing Tokens in C
Problem
Given a sentence, S, print each word of the sentence in a new line.
Input Format
The first and only line contains a sentence, S.
Constraints
- 1<=len(s)<=1000
Output Format
Print each word of the sentence in a new line.
Input 0
This is C
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.
Input 1
Learning C is fun
Output 1
Learning C is fun
Input 2
How is that
Output 2
How is that
Printing Tokens in C Solutions
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *s; s = malloc(1024 * sizeof(char)); scanf("%[^\n]", s); s = realloc(s, strlen(s) + 1); int len = strlen(s); for(int i = 0; i < len; i++) { if(s[i] == ' ') { printf("\n"); } else { printf("%c", s[i]); } } free(s); return 0; }
Disclaimer: The above Problem (Printing Tokens in C ) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.