Hello everyone,

I have a code that use function to find and remove all occurrences of a string within a string (substring pending) and return the number of removals: I need to remove "xyz" in "abdxyzdxyz" the result should be "abdd" and a return of 2 removed. I used the debuger step into, it keep shown that "Removals = o" and the sbstring value = "abd", missing one character of "d". I can't see if I missed something, can some one help me see what I did wrong please.

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

void Remove(int );
//string str1 = "abdxyzdxyz";
//string str2 = "xyz";
char* str1 = "abdxyzdxyz";
char* str2 = "xyz";

//current_length = str1.length();

int main()
   {
   char substring[4];
   int prev_occurence = 0;
   int removals = 0;
   int removed_occurence=0;
   

   for(int i=0; i < strlen(str1); i++)
   {
	   if(i % strlen(str2) == 0)
	   {
		   if(prev_occurence == 0)
			memcpy(&substring, &str1[prev_occurence], 3);
		   else
			   memcpy(&substring, &str1[prev_occurence], 3);
			substring[3] = '\0';
		   cout << substring;
		   removed_occurence = prev_occurence;
		   prev_occurence = i;
		   if(strcmp(str2, substring) == 0)
		   //if(strcmp(str2,substring) == 0)
			{
				
				Remove(removed_occurence);
				//Remove(prev_occurence - strlen(str2));
				//removed_occurence = prev_occurence - strlen(str2);	
				removals++;
			}
	   }
   }

   cout << "Removals = " << removals << endl;
getch();
   return 0;
   } 
void Remove(int prev_occurence)
{
	char another_string[50];
	//current_length -= 3;

	//another_string.copy(str1, str1.length());
	memcpy(&another_string, &str1[prev_occurence], strlen(str1) - prev_occurence);
	another_string[strlen(str1) - prev_occurence] = '\0';
	memcpy(&str1[prev_occurence], another_string, strlen(another_string));
	str1[prev_occurence + strlen(another_string) + 1] = '\0';
}:icon_sad:

try this:

string s1 = "abdxyzdxyz";
string s2 = "xyz";
size_t pos;
while( (pos = s1.find(s2)) != string::npos)
{
      string s3 = s1.substr(0,pos);
      if( (pos+s2.length() + 1) < s1.length())
        s3 += s1.substr(pos+s2.length()+1);
      s1 = s3;
}

Hello Ancien Dragon,

Good to hear from you again, I have added the code you have show below and I got errors:

void Remove(int );
//string str1 = "abdxyzdxyz";
//string str2 = "xyz";
char* str1 = "abdxyzdxyz";
char* str2 = "xyz";

size_t pos;											//****************************
while( (pos = str1.find(str2)) != string::npos);	//I added this part to see if*
{													//it work.		     		 *
      string str3 = str1.substr(0 , pos);			//			        		 *
      if( (pos + str2.length() + 1) < str1.length())//							 *
        str3 += str1.substr(pos + str2.length() + 1);//**************************/
      str1 = str3;
}

//current_length = str1.length();

int main()

[error]
error C2059: syntax error : 'while'
error C2447: '{' : missing function header (old-style formal list?)
warning C4018: '<' : signed/unsigned mismatch
[/error]

try this:

string s1 = "abdxyzdxyz";
string s2 = "xyz";
size_t pos;
while( (pos = s1.find(s2)) != string::npos)
{
      string s3 = s1.substr(0,pos);
      if( (pos+s2.length() + 1) < s1.length())
        s3 += s1.substr(pos+s2.length()+1);
      s1 = s3;
}

This should work, not tested...

#include <iostream>
#include <string>

class Foo
{
public:
  void remove ( std::string input, std::string rem )
  {
    std::cout << "Original string is " << input << std::endl;
    std::cout << "After removing " << rem << " is:";
    int i = 0;
    int appearances = 0; 

    for ( i = input.find ( rem, 0 ); i != std::string::npos; 
          i = input.find ( rem, i ) )
    {
      appearances++;
      input.erase ( i, rem.length() );
      i++;
    }
    
    std::cout << input << std::endl;
    std::cout << "Number of removals " << appearances << std::endl;
  }
};

int main()
{ 
  Foo bar;
  bar.remove("abdxyzdxyz","xyz");
  
  std::cin.get();
}

My output

Original string is abdxyzdxyz
After removing xyz is:abdd
Number of removals 2

Hi Ancien Dragon,

Want to thank you very much for all your help, I got the code to compiled and working, what is this line do??

for ( i = input.find ( rem, 0 ); i != std::string::npos;

what is "npos", I don't see it declare any where. I need to use "char* strings" instead of "std::string", the instructor do not like us to use "std::string". I know this code work and I don't want to change anything, but for now we have too change it to use "char* string", how can I do that in this code??? when I done with the class then I can use what ever I want ha ha.

Regards,

>>I have added the code you have show below and I got errors:
Because you didn't put the code inside a function

npos is declared in <string> Look at the description of find() method and you will see that it returns npos when the string is not found.

For character arrays you can use strstr() declared in string.h Your text book should have the description for that function or you can look it up in google.

This article has been dead for over six months. Start a new discussion instead.