Last active
December 20, 2015 14:39
-
-
Save sumanth232/460a6ad5dad4367eaf41 to your computer and use it in GitHub Desktop.
split function in C ( written by myself )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define len 200 | |
typedef struct _string | |
{ | |
char tokens[30][200]; // tokens can be treated as an array of strings | |
int no_of_tokens; | |
}str_info; | |
str_info split(char* str, char* delim) // doesnt do any changes to string pointed by str | |
{ // splits the string based on the delimiters provided and stores in "struct _string" | |
char tempstr[len]; | |
int size; | |
str_info info; | |
int i; | |
for (i = 0; i < 30; ++i) | |
{ | |
memset(info.tokens[i],'\0',sizeof(info.tokens[i])); | |
} | |
size=0; | |
strcpy(tempstr, str); | |
char* ptr = strtok(tempstr, delim); | |
while(ptr != NULL) | |
{ | |
strcpy(info.tokens[size++],ptr); | |
//printf("%s\n",info.tokens[i]); | |
ptr = strtok(NULL,delim); | |
} | |
info.no_of_tokens = size; | |
return info ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment