I'm trying to split a string up based on some delimiter. I'm aware of strtok and the newer strsep but I'm having some trouble with boundary cases. For example, I want to split a string with delimiter /.
I'm basically using the program to traverse a path (implementing ls for class). My issue is with say the following couple of strings:
char *a = "toplevel/secondlevel/thirdlevel/"char *b = "toplevel/secondlevel/thirdlevel" (note no slash at the end)char *c = "toplevel"char *d = "/toplevel"
I'm ending up with some cases where I have an empty string after using strsep and having many if statements for different checks is too messy. Here is the code that I'm using: while ((next_level = strsep(&whole_path, "/")) != NULL) {...} and I'm aware that strsep destroys the original string, which is alright.
Optimally I'd like to have one of the two solutions (clean if possible), preferably the 1st:
1. strip the / from the whole string, beginning and end;
2. strsplit working without the many issues of boundary cases
Any help would be appreciated. My assignment is about filesystems and other matters but this small issue has caused me many hours of heartache and messy code. Thanks!
Aucun commentaire:
Enregistrer un commentaire