Printing Tokens in C - Online Judge

Latest

This is an Online Judge Solution Base Site. We can discuss & Solve any contest solution in Programming.

Sunday, March 22, 2020

Printing Tokens in C

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

Solution in C:-

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

int main() {

char *s, *t;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);

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

No comments:

Post a Comment

Thanks..