-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseString.c
More file actions
43 lines (40 loc) · 922 Bytes
/
parseString.c
File metadata and controls
43 lines (40 loc) · 922 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
// "Preloaded Code" (do NOT modify!)
typedef struct node {
int data;
struct node *next;
} Node;
Node *parse(char *string) {
//TODO: return the linked list represented by the provided string
char *tmp;
Node *head;
Node *iter;
tmp = string;
while(isspace(*tmp))
tmp++;
if (!isdigit(*tmp))
return (NULL);
head = (Node *)malloc(sizeof(Node));
head->data = atoi(tmp);
head->next = NULL;
iter = head;
while(*tmp != '\0')
{
while(*tmp != '>' && *tmp != '\0')
tmp++;
while(!isdigit(*tmp) && *tmp != '\0')
tmp++;
if (*tmp != '\0' && isdigit(*tmp))
{
iter->next = (Node *)malloc(sizeof(Node));
iter = iter->next;
iter->data = atoi(tmp);
iter->next = NULL;
}
}
return head;
}