-
Notifications
You must be signed in to change notification settings - Fork 0
/
Printing_Tokens.c
68 lines (50 loc) · 1.17 KB
/
Printing_Tokens.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// // Printing Tokens
// // Problem
// Given a sentence, , print each word of the sentence in a new line.
// Input Format
// The first and only line contains a sentence, .
// Constraints
// 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
#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);
int len = strlen(s);
//Write your logic to print the tokens of the sentence here.
for(int i=0; i < len ; i ++){
if(s[i] == ' '){
printf("\n");
}else {
printf("%c", s[i]);
}
}
return 0;
}