-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hackerrank_Problem_11.c
34 lines (33 loc) · 1018 Bytes
/
Hackerrank_Problem_11.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
/*
Problem 11: Printing Tokens.
Easy Approach:
Steps:
1) Initially we include string library to get some benfits of it.
2) Declare dynamic memory for character array, because input can be of any length that is why
dynamic memory id used.
eg- malloc(1024 * sizeof(char));
here 1024 is byte allocated.
sizeof - function for getting the size of char array.
3) Now we iterate over the char array.
4) But the twist is that we have to print it on new line after whitespace.
5) So, with the help of if-else statement we check for whitespace if there we give line break,
otherwise we simply print the string for array.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
for(int i=0;i<strlen(s);i++){
if(s[i]==' '){
printf("\n");
}
else{
printf("%c",s[i]);
}
}
return 0;
}
//Thank You By: Harsh Udai.