Riley
06-01-2002, 02:24 PM
Okay, I opened the header file (string.h) and could not find a substr function and I need to take a string apart... What function(s) do I use?
|
Click to See Complete Forum and Search --> : Strings in C Riley 06-01-2002, 02:24 PM Okay, I opened the header file (string.h) and could not find a substr function and I need to take a string apart... What function(s) do I use? sans-hubris 06-01-2002, 02:29 PM Use strtok to break up a string. Riley 06-01-2002, 02:35 PM Well, it's already delimited I'm trying to turn it into multiple strings with the delimitors... debiandude 06-01-2002, 08:01 PM This seems to work. #include <stdio.h> #define MAX 10 char **split(char *delim, char *str); int main(void) { char src[15] = "this:is:a:test"; char *delim = ":"; char **list; list = split(delim, src); while(*list) { print("%s",*list++); } return 0; } char **split(char *delim, char *str) { static char *list[MAX]; char *begin = str; int length = strlen(str); int flag = 0; int i = 0; while(*str) { if(*str == *delim) *str = '\0'; str++; } while(length && (i<MAX)) { if((*begin != '\0') && (!flag)) { list[i++] = begin; flag = 1; } else if(*begin == '\0') { flag = 0; } begin++; length--; } return list; } justlinux.com
Copyright Internet.com Inc. All Rights Reserved. |