views:

614

answers:

5

I only have access to 'C' and need to replace characters within a character array. I have not come up with any clean solutions for this relatively simple procedure.

I am passed a character array, for example:

char strBuffer[] = "/html/scorm12/course/course_index.jsp?user_id=100000232&course_id=100000879&course_prefix=ACQ&version=2&scorm_version=3&roster_id=100011365&course_name=Test%20Course%201.2&mode=browse&course_number=0000&mode_id=1";

I need to modify this buffer to replace all the & with &. The resulting buffer does not have to overwrite strBuffer (a new buffer can be created).

Any suggestions?

Edit:

In the past I have done the strstr function in a loop, but was looking for a simpler solution, perhaps the C equivalent to the String.Replace method.

Edit:

For my immediate needs, the following is all that I need.

char strBuffer[] = "/html/scorm12/course/course_index.jsp?user_id=100000232&course_id=100000879&course_prefix=ACQ&version=2&scorm_version=3&roster_id=100011365&course_name=Test%20Course%201.2&mode=browse&course_number=0000&mode_id=1";
char strTemp[1024];
char *s = (char*)strBuffer;
int i=0;

while (*s)
{
    strTemp[i++] = *s;
    if (strncmp(s,"&",5) == 0)
    {
        s += 5;
    }
    else
        s++;
}
strTemp[i] = 0;

Future modifications:

  • Create a utility function to store this function.
  • Pass the search string as a parameter.
  • Determine the search string's length, so the hardcoded 5's can be removed.
  • Dynamically allocate the strTemp variable.
  • Error checking for empty strings and chars not found.
A: 

Here is all you need to know.

Nikolai N Fetissov
While I agree that the OP appears not to know C, and should learn the language if he/she has to use it, your answer doesn't help anyone. Unless you're gunning for the "Peer Pressure" badge.
Chris Lutz
+2  A: 

Allocate another buffer, either on the stack or the heap, and then copy the string into the new buffer 1 character at a time. Make special handling when you encounter the & character.

Brian R. Bondy
That's an array literal, and it can be changed because it's stored in an array. A `char *c = "string";` can't be. But yes, you shouldn't change it to safeguard against making this kind of mistake in the future.
Chris Lutz
Actually, in the example posted (unless it's been edited after your answer) strBuffer is a buffer that is writable and is initialized by copying the literal string into the buffer.
Michael Burr
sorry you're right it is an array literal: sizeof(strBuffer) == 249
Brian R. Bondy
I would say definitely allocate the new buffer on the heap. This kind of functionality is definitely something that should be abstracted away into a separate function, so you'd need to use the heap to pass things around (or require the caller pass in his/her own buffer).
Chris Lutz
I prefer the passing in your own buffer to a helper function way.
Brian R. Bondy
+3  A: 

C isn't noted for it's ease of use, especially when it comes to strings, but it has some rather nice standard library functions that will get the job done. If you need to work extensively on strings you'll probably need to know about pointers and pointer arithmetic, but otherwise here are some library functions that will undoubtedly help you:

  • strchr() to find a character (say, '&') in a string.
  • strcmp() and strncmp() to compare two strings.
  • strstr() to find a substring in a string (probably easier and faster than using the strchr()/strcmp() combination).
  • malloc() to allocate a new string.
Chris Lutz
Any reason for the downvote here?
Chris Lutz
Thanks ... there are a whole lot of standard C functions that can be quite handy!
Edward Leno
+4  A: 

Basically, you need to:

  • use the strstr() function to find the "&"s
  • copy characters to the resulting buffer up to the position found
  • skip 4 characters
  • repeat until NUL
pmg
You might use `strstr()` but you can also use `strchr()` to find the '
Chris Lutz
Using strstr() is more generic and convenient. It may also be faster, depending on how strstr() is implemented. I recommend his approach. If speed is really a concern, more sophisticated algorithm like KMP is preferred; but I guess Edward Leno just needs something convenient.
@Chris In this case you're essentially just manually doing the strstr() work inside of your conditional block. I'd think it would be wiser to use the standard function to mitigate potential errors.
Andrew Song
+1  A: 
char *s = (char*)strBuffer;
char sClean[strlen(strBuffer) + 1]; /* +1 for null-byte */
/* if above does not work in your compiler, use:
    char *sClean = (char*)malloc(sizeof(strBuffer) + 1);
*/
int i=0;
while (*s)
{
    sClean[i++]= *s;
    if ((*s == '&') && (!strncmp(s, "&", 5)) s += 5;
    else s++;
}
sClean[i] = 0;
slashmais
Variable-length arrays (like `char sClean[strlen(strBuffer)];` above) are C99 only, and aren't fully supported by a large number of compilers.
Chris Lutz
Noted. If Q's compiler can;t do it, Q can use malloc.
slashmais
HTML entities: http://www.w3.org/TR/html4/sgml/entities.html#h-24.2.1
pmg
Chris Lutz
Another problem is the size of sClean is strlen(strBuffer). If there is nothing to replace, the code above has memory violation.