Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #define NMAX 100
- #define SEP ','
- int main(void) {
- static char s[NMAX];
- if (fgets(s, NMAX, stdin) == NULL) {
- perror("");
- exit(EXIT_FAILURE);
- }
- s[strlen(s) - 1] = 0;
- char *p1 = s, *p2 = NULL;
- bool found = true;
- while (found) {
- char *s = NULL;
- p2 = strchr(p1, SEP);
- if (p2) {
- int len = p2 - p1 + 1;
- s = malloc(sizeof(char) * len);
- strncpy(s, p1, len - 1);
- s[len - 1] = 0;
- p1 = p2 + 1;
- } else {
- int len = strlen(p1) + 1;
- s = malloc(sizeof(char) * len);
- strcpy(s, p1);
- found = false;
- }
- if (!*s) {
- free(s);
- continue;
- }
- puts(s);
- free(s);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement