Printing Tokens || Hacker Rank

 Question 13 || Hacker Rank

Printing Tokens || Hacker Rank

Printing Tokens



Given a sentence, , print each expression of the sentence in another line.

 

Input Arrangement

The solitary line contains a sentence, .

 

Requirements

Yield Arrangement

Print each expression of the sentence in another line.

 

Test Information 0

This is C

Test Result 0

 

This

is

C

 

Clarification 0

In the given string, there are three words ["This", "is", "C"]. We need to print every one of these words in another line.

 

Test Info 1

It is amusing to Learn C

 

Test Result 1

Learning

C

is

fun

 

Test Information 2

How is that

 

Test Result 2

How

is

that



#include <stdio.h>
#include <string.h>

int main() 
{
    char sentence[1000];
    fgets(sentence, sizeof(sentence), stdin);


    if (sentence[strlen(sentence) - 1] == '\n'
{
    sentence[strlen(sentence) - 1] = '\0';
}

    char *token = strtok(sentence, " ");

while (token != NULL) 
{
    printf("%s\n", token);
    token = strtok(NULL, " ");
}

    return 0;
}


Output:

Printing Tokens




Post a Comment

0 Comments