Algorithm


Problem Name: Printing Tokens

Problem Link: https://www.hackerrank.com/challenges/printing-tokens-/problem?isFullScreen=true

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

x
+
cmd
This is C

Output

x
+
cmd
This is C
Advertisements

Demonstration


Previous
[Solved] Array Reversal in C solution in Hackerrank - Hacerrank solution C
Next
[Solved] Digit Frequency in C solution in Hackerrank - Hacerrank solution C