Saturday, 31 August 2013

How to cut a string using 2 delimiters

How to cut a string using 2 delimiters

How to cut a string using 2 delimiters in C?
I'm getting a string from the user in this platform:
cp <path1> <path2>
I need to get the pathes into a new string (each path to one string).
I tried to use strstr and strtok but it doesn't work.
I don't know the length of the pathes. I also just know that they are
starting with " \" (this are the delimiters that I have (space + \)).
this is what i tried
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
char *c;
char *ch =malloc (1024);
while (strcmp(ch,"exit")){
scanf("%[^\n]%*c",ch); //what was the input (cp /dor/arthur /king/apple)
c=malloc (sizeof(strlen(ch)+1));
strcpy(c,ch);
char *pch=strtok(c," //");
printf("this is : %s \n",pch); //printed "this is: cp"
}
}

No comments:

Post a Comment