C + + Implements a method that replaces a character in a string with another string _c language

Source: Internet
Author: User
Tags strlen

This article describes the C + + implementation of a string of characters to replace the method of another string, shared for everyone to reference. The specific methods are as follows:

Topic Requirements:

Replace each space in the in-place implementation string with "%20", such as entering "We are happy" and outputting "We%20are%20happy"
The replaced string is certainly not just a space, it's just an example
This is a good topic, Baidu interview is a problem, the topic is not difficult, but the problem must be considered comprehensively. The following implementation code is given here:

#include <iostream> #include <vector> #include <cassert> using namespace std; int Findnumberfirst (const char *STR, const char *dest, vector<int>& Pvec) {if (str = NULL | | dest = NULL) r

 Eturn 0;
 int pos = 0;
 int lenstr = strlen (str);

 int lendest = strlen (dest);

 if (Lenstr < lendest) return 0;
 int count = 0;
  while (POS <= lenstr-lendest) {if (strncmp (str + pos, dest, strlen (dest)) = = 0) {pvec.push_back (POS);
  POS + lendest;
 count++;
 else {pos++;
} return count;
 int findnumberlast (const char *STR, const char *dest, vector<int> &pvec) {if (str = NULL | | dest = NULL)

 return 0;
 int strLen = StrLen (str);

 int destlen = strlen (dest);

 if (StrLen < Destlen) return 0;
 int pos = 0;
  while (POS <= strlen-destlen) {if (strncmp (str + pos, dest, StrLen (dest)) = = 0) {pos = Destlen;
 Pvec.push_back (POS-1);
 else {pos++;
} return Pvec.size (); } void Replacearray (char*STR, const char *SRC, const char *dest) {if (str = NULL | | src = NULL | | | dest = NULL) return;
 Vector<int> Pvec;
 int strLen = StrLen (str);
 int srclen = strlen (src);

 int destlen = strlen (dest);

 if (StrLen < Srclen) return;
 int posbefore = 0;

 int posafter = 0;
 if (Srclen < destlen) {int count = findnumberlast (str, SRC, pvec);
 
 if (count <= 0) return;
 Posafter = StrLen + count * (Destlen-srclen)-1;

 Posbefore = strLen-1;
  while (Count > 0 && posbefore >= 0) {if (pvec[count-1] = = Posbefore) {posafter = Destlen;
  strncpy (str + posafter + 1, dest, strlen (dest));
  count--;
  posbefore--;
  else {str[posafter--] = str[posbefore--];
 }} else if (StrLen > Destlen) {int count = Findnumberfirst (str, SRC, pvec);

 if (count <= 0) return;
 Posafter = 0;

 Posbefore = 0;
 int i = 0;
  while (Count >= 0 && Posbefore < strLen) {if (Count > 0 && pvec[i] = = Posbefore) {strncpy (str + posafter, dest, strlen (dest));
  Posafter + = Destlen;
  count--;
  Posbefore + = Srclen;
  i++;
  else {str[posafter++] = str[posbefore++];
 } Str[posafter] = ' I ';
 } void Main () {char *str = new CHAR[100];
 if (str = NULL) return;

 memset (str, ' n ', 100);
 const char *SRC = "";
const char *dest = "%20";

 CASE1: only 1 spaces strcpy (str, "");
 Replacearray (str, SRC, dest);

 cout << "str:" << str << Endl;
 Replacearray (str, dest, SRC);

cout << "str:" << str << Endl;

 Case2: Two spaces strcpy (str, "");
 Replacearray (str, SRC, dest);

 cout << "str:" << str << Endl;
 Replacearray (str, dest, SRC);

cout << "str:" << str << Endl;

 CASE3: normal situation strcpy (str, "We are Happy");
 Replacearray (str, SRC, dest);

 cout << "str:" << str << Endl;
 Replacearray (str, dest, SRC);

cout << "str:" << str << Endl; CASE3: Space in front strcpy (str, "We are happy");
 Replacearray (str, SRC, dest);

 cout << "str:" << str << Endl;
 Replacearray (str, dest, SRC);

cout << "str:" << str << Endl;

 CASE4: space in posterior strcpy (str, "We are Happy");
 Replacearray (str, SRC, dest);

 cout << "str:" << str << Endl;
 Replacearray (str, dest, SRC);

cout << "str:" << str << Endl;

 CASE4: No space strcpy (str, "wearehappy");
 Replacearray (str, SRC, dest);

 cout << "str:" << str << Endl;
 Replacearray (str, dest, SRC);

cout << "str:" << str << Endl;

 CASE5: Both sides are as strcpy (str, "We are Happy");
 Replacearray (str, SRC, dest);

 cout << "str:" << str << Endl;
 src = "%20";
 ASSERT (Dest = = "%20");
 Replacearray (str, dest, SRC);

 cout << "str:" << str << Endl;}

Analysis of the above code, a very interesting case is srclen and destlen or large or small cases, the boundary conditions of the determination is not the same. For example we are happy as an example, from the back to the forward copy when count=2.

When Count=0, the first space is replaced exactly, we do not repeat the copy. But for the copy from the post, when the count=0, the final face of the happy will not be copied.

I hope that the example mentioned in this article is helpful to the learning of C + + program algorithm design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.