Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting double quotes

Status
Not open for further replies.

klose86

Programmer
Aug 1, 2006
13
CR
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?

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.
 
You're not terminating your new string with '\0'.
 
I don't know if that solves the problem.

Where should I terminate the string with '\0'? During the loop or after it finishes?

Cause, after the loop ends, the new string already contains the garbage I'm trying to avoid. So, it would make no sense to add a '\0' in the last position of the string, since I'm not deleting the garbage the string got at its last positions.

THanks for the help
 
All variables contain garbage until you set them to a specific value.
The reason the strings contain garbage at the end is because the only way C knows where the end of a string is is when it finds a NULL. When you allocate a new string in C, you need to allocate enough space for the text you want to put in the string, plus 1 more character for the NULL at the end.

Also, rather than copying each character yourself in a loop, it's much easier to call the strncpy() function to copy a specified number of characters from one string to another. strncpy() also adds the NULL to the end of your new string (unless your new string is too small).
Code:
char* Delete( char* a1 )
{
   char* a2;
   int length = strlen( a1 );

   if ( (a1[0] == '"') && (a1[length - 1] == '"') )
   {
      a2 = (char*)malloc( length - 1 );

      if ( a2 == NULL )
      {
         printf( "Out of memory!" );
         exit( 1 );
      }

      strncpy( a2, &(a1[1]), length - 1 );
   }
   else
   {
      printf( "Error!  The string passed to Delete() isn't quoted." );
      exit( 1 );
   }

   return a2;
}
 
> strncpy() also adds the NULL to the end of your new string (unless your new string is too small).
Which it is in this case, since you're using it to extract a sub-string.

Plus you copy the trailing " as well
Code:
      strncpy( a2, &(a1[1]), length - 2 );
      a2[length-2] = '\0';

Casting malloc in C doesn't really do anything useful either.
At worst, it hides the fact that you failed to prototype the function correctly (by including stdlib.h), or it hides lazy use of using C++ to compile C programs.

--
 
Well, Salem is totally right!!

It worked pretty much fine when I added the a2[length-2] = '\0'; to the code.

Thank you guys a lot for your help, now I've learned something new :)


Salem rlz!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top