Hacker Rank C

Printing Tokens in C HackerRank Solution

Hello Friends in this article i am gone to share Hackerrank C programming Solutions with you. | Printing Tokens in C HackerRank Solution.

Objective

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

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

Hackerrankย  C programming Solutions

Printing Tokens in C HackerRank Solution

#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 (char *c = s; *c != NULL; c++) {
    if (*c == ' ') {
        *c = '\n';
    }
}
printf("%s", s);
    
    return 0;
}

 


Tokens in C

Tokens are some of the most important elements used in the C language for creating a program. One can define tokens in C as the smallest individual elements in a program that is meaningful to the functioning of a compiler.

A token is the smallest unit used in a C program. Each and every punctuation and word that you come across in a C program is token. A compiler breaks a C program into tokens and then proceeds ahead to the next stages used in the compilation process.