Hi everyone
I'm writing a program in C that reads a text file and search for strings with doble quotes and the deletes the double qoutes.
For example: "string" => string
I wrote this function, but it seems to be working just with the first double quoted string it finds in the file.
For example if I got:
The "book" will be "released" in the "next" months.
The function converts "book" in to book, but it adds garbage to the other double qouted strings, like this:
book
releasedÑ·5@
nextÇÑ:·@
Any idea?
Thanks everyone for your help.
I'm writing a program in C that reads a text file and search for strings with doble quotes and the deletes the double qoutes.
For example: "string" => string
I wrote this function, but it seems to be working just with the first double quoted string it finds in the file.
For example if I got:
The "book" will be "released" in the "next" months.
The function converts "book" in to book, but it adds garbage to the other double qouted strings, like this:
book
releasedÑ·5@
nextÇÑ:·@
Any idea?
Code:
char* Delete(char* a1){
if(a1[0]== '"' && a1[(strlen(a1)-1)] == '"'){ //check for the double qoutes
int length = strlen(a1); //calculate length of string
char* a2 = (char *)malloc(length-2); //the resulting string without double quotes, that's 2 chars less
int i = 1;
int j = 0;
while(i< (length-1)){
a2[j] = a1[i]; //copy each character
i++;
j++;
}
return a2;
}
else{ printf("ERROR\n");
exit(0);
}
}
Thanks everyone for your help.