Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

arrays - Remove characters from a string in C

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.

EDIT:

I created a blog post to detail the steps and provide a more flexible solution:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...