I wanted to parse an input string (path) into the array.
For example, if the input is
cd("C/Hra/Halo 4/Hra");
The array should contains {"C","Hra","Halo 4","Hra"}, but it contains {"C","Hra","Halo 4,"HraC"}.
My friend suggested me to put "\0" for each string, but it doesn't work.
Can someone please suggest me how to repair it?
My code:
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
void cd(char* path){
printf("Path %s \n",path);
int count = 0;
int i = 0;
int length = (int)strlen(path);
for(i;i<length;i++){
if(path[i]=='/')
count ++;
}
char foldersName[count][255];
char str[strlen(path)];
strcpy(str,path);
char * pch;
pch = strtok (str,"/");
count = 0;
while (pch != NULL)
{
strcpy(foldersName[count],pch);
strcat(foldersName[count],"\0");
pch = strtok (NULL, "/");
count ++;
}
printf("----------------Tokens-----------\n");
for(i =0;i<count;i++){
printf("%s \n",foldersName[i]);
}
}
int main(int argc, char *argv[])
{
cd("C/Hra/Halo 4/Hra");
}
char foldersName[count][255];char str[strlen(path)];-->char foldersName[count+1][255];char str[length+1];