In this HackerRank Printing Tokens problem in C programming, we need to make a program in c programming in which we have given a sentence and we need to print each word of the sentence in a new line.


HackerRank Printing Tokens in c problem solution

HackerRank Printing Tokens in c problem solution

 
#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;
}