Remove characters from string in C

I only have access to "C" and you need to replace the characters in the character array. I have not come up with any simple solutions for this relatively simple procedure.

I was given an array of characters, 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 change this buffer to replace everything &with &. The resulting buffer should not overwrite strBuffer (you can create a new buffer).

Any suggestions?

Edit:

In the past, I ran the strstr function in a loop, but searched for a simpler solution, possibly C, equivalent to the String.Replace method.

Edit:

For my immediate needs I need everything 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 changes:

  • Create a utility function to save this function.
  • Pass the search string as a parameter.
  • Define the length of the search bar so that you can remove hardcoded 5.
  • strTemp.
  • .

EDIT:

, :

http://www.solutionmaniacs.com/blog/2012/11/25/c-removereplace-characters-in-a-string.html

+3
4
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;
+1

C , , , . , , , , , , , :

+5

, :

+4

Allocate another buffer either on the stack or in the heap, and then copy the line to the new buffer 1 character at a time. Perform special processing when you encounter a symbol &.

+2
source

Source: https://habr.com/ru/post/1718934/


All Articles