Algorithm
Problem Name:
In this HackerRank Functions in C programming problem solution,
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.
Sample Input 0
This is C
Sample Output 0
This
is
C
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("% [^\n]", s);
s = realloc(s, strlen(s) + 1);
//Write your logic to print the tokens of the sentence here.
for (int i = 0; i < strlen(s); i++){
if (s[i] == ' ') {
printf("\n");
}
else{
printf("%c", s[i]);
}
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
This is C
Output
This
is
C