CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2006
    Posts
    384

    Character replacement in string

    Hi,

    Consider a string "C:\Mine\Data" which is stored in a location pointed to by char *p.

    I need to replace '\' with '\\' in the string and am not allowed to use the std::string type. This code is written in C and hence cannot use std::string.

    Can someone please suggest an efficient method to achieve the functionality ?

  2. #2
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: Character replacement in string

    Code:
    char* szTemp = malloc(2*strlen(p)+1);
    char* d = szTemp;
    for(char* s = p; *s != '\0'; ++s)
    {
    	*d = *s;
    	++d;
    	if(*s == '\\')
    	{
    		*d = '\\';
    		++d;
    	}
    }
    *d = '\0';
    strcpy(p, szTemp);
    free(szTemp);
    Be careful with this code. You need to have allocated enough memory for p, because the string will grow by the number of \ replaces. If it is too small you will corrupt the stack.
    Last edited by philkr; July 18th, 2006 at 09:19 AM. Reason: It has to be C
    Please don't forget to rate users who helped you!

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Character replacement in string

    Quote Originally Posted by humble_learner
    Can someone please suggest an efficient method to achieve the functionality ?
    It depends on the requirement.
    Can you accept an in-place modification of the string.
    It means two things:
    • You'll loose the old string.
    • The old string must have enough space to hold the new characters.

    Otherwise, the most user-friendly interface would be to return malloc'ed memory.
    In the second case:
    1)Count the number of backshlashes in the string in order to allocate the right memory space (including the null terminator).
    2)Lineary parse the old string from left to right, and when encountering a backslash, put two backslashes in the output string.
    3) Put the null terminator.
    4) Returns the string.

    For inplace modification, you can write these two routines.
    Code:
    size_t CountBackslashes(const char* str);
    void BackslashProtect(char* DestinationBuffer, const char* SourceBuffer,size_t BackslashCount);
    BackslashProtect could also be used to write the user-friendly malloc'ing version.
    It should parse the string backward, and use BackslashCount in order to know the initial shift.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Character replacement in string

    phillkr : It is C : The OP can't use std::string, nor new[]
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  5. #5
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: Character replacement in string

    Quote Originally Posted by SuperKoko
    phillkr : It is C : The OP can't use std::string, nor new[]
    Well, what shall I say? Force of habit!
    Please don't forget to rate users who helped you!

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Character replacement in string

    Quote Originally Posted by humble_learner
    Hi,

    Consider a string "C:\Mine\Data" which is stored in a location pointed to by char *p.

    I need to replace '\' with '\\' in the string
    Now for the question no one has asked yet:

    Why do you need to do this replacement? In C and C++, any input that consists of a backslash automatically is converted to two backslashes. Unless you are trying to replace "\\" with "\\\\", what you're asking doesn't make sense.

    For example, the string you wrote "C:\Mine\Data" consists of the following characters:
    'C' ':', '\M', 'i', 'n', 'e', '\D', 'a', 't', 'a'
    The third character is a "\M", it isn't '\'. In other words, the '\' takes the next letter and makes it a single control character.

    When you write the code, within your program source you need to specify two backslashes. If the string is coming from another function as exactly as you've described, your program has a bug -- it should never be that way. If the string is coming in as input from a file, keyboard, socket, whatever, then the string needs no translation.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured