strcat issues

This is a discussion on strcat issues within the C Programming forums, part of the General Programming Boards category; I'm attempting to parse a string. What I need to do is have a function turn any instance of apostrophes ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    Unhappy strcat issues

    I'm attempting to parse a string. What I need to do is have a function turn any instance of apostrophes (') or quotation marks (") and put a backslash(\) in front of them. Like this:

    From:
    I can't hear what "you" said.

    To:
    I can\'t hear what \"you\" said.

    Here is my function so far, which is tacking on the needed adjustment to the end of the string, not where I need it to go.

    Code:
    char *fixQuotes(char *theString)
    {
    	char buf[2500];
    	int length = 0;
    
    	snprintf(buf, sizeof(buf), "%s", theString );
    
    	length = ((strlen(buf)));
    
    	while(length)
    	{		
    		if (buf[length] == '\'')
    		{
    			strcat(buf, "\\\'");
    			length--;
    			continue;
    		}
    		if (buf[length] == '\"')
    		{
    			strcat(buf, "\\\"");
    			length--;
    			continue;
    		}
    		length--;
    	}
    	return _strdup(buf);
    }
    So taking the above example string, if I ran it thru this current function, I end up with:

    I can't hear what "you" said.\"\"\'

    Again, I need it to read:
    I can\'t hear what \"you\" said.


    Any and all suggestions/criticisms/help would be very much appreciated.

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    strcat combines 2 strings together by putting the new addition at the end. This means that, even though you are stepping through the original string, when you come to a " or a ' you are adding \" or \' to the end rather than inserting the \ as it appears you are expecting to happen.

    You might want to re-think how you are going about this. If you want to use strcat() still, you might want to break up the string so that you are concatenating I can't hear what "you with \" said. You could use strtok() to make your string into tokens that are separated by the " and then strcat() them back together with \'s in between, and then do a second pass where you strtok() based on '.

    Another approach would be to step forward through your source string and copy character by character to a buffer, and each time you encounter a ' or a ", put a \ in the destination string before you copy the ' or ".
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    I think I'll use your strtok() suggestion. I'll post back with my results. Thank you for your suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 09:00 AM
  2. strcat the inside of a structure
    By ebullient in forum C Programming
    Replies: 3
    Last Post: 12-09-2005, 04:58 PM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 11:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21