Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,064 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,593 people online right now. Registration is fast and FREE... Join Now!




function to remove all occurrences

 
Reply to this topicStart new topic

function to remove all occurrences, copiled but wrong result

KMH
20 Apr, 2008 - 08:21 PM
Post #1

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

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 substring 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.

CODE

#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';
}


This post has been edited by KMH: 20 Apr, 2008 - 08:22 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Function To Remove All Occurrences
20 Apr, 2008 - 10:43 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
Quick glance over: Your program is overwriting memory addresses it shouldn't be. i.e. nasty memory leak down the road if the debugger didn't throw up a nasty warning when the program is executed.
User is online!Profile CardPM
+Quote Post

KMH
RE: Function To Remove All Occurrences
21 Apr, 2008 - 08:09 PM
Post #3

D.I.C Head
**

Joined: 23 Mar, 2008
Posts: 80

OK I have overloaded the mem, it is easy to write this program using find(), but I need to use char* string to find the "xyz", I'm lost when it come to pionters, how would I change the code below using the (char* string) function and to get the same output.
CODE


#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("abdxyzdxyz");
  string str2 ("xyz");
  size_t found;
  
    cout << "The Original string: " << str << endl;
    found=str.find(str2);
    if (found!=string::npos)
    cout << "Total of 'xyz' found: " << int(found) << endl;

  
cin.get();
  return 0;
}



QUOTE(KYA @ 20 Apr, 2008 - 11:43 PM) *

Quick glance over: Your program is overwriting memory addresses it shouldn't be. i.e. nasty memory leak down the road if the debugger didn't throw up a nasty warning when the program is executed.


User is offlineProfile CardPM
+Quote Post

iGV
RE: Function To Remove All Occurrences
21 Apr, 2008 - 09:49 PM
Post #4

New D.I.C Head
*

Joined: 20 Apr, 2008
Posts: 2

TRY this...


int _tmain(int argc, _TCHAR* argv[])
{
char* abc=("abcxyzdxyz");
char* sub=("xyz");
char* temp=new char[20];
int len = strlen(abc);
int count=0,i=0,v=0;
while(len-3)
{
if(abc[i]==sub[0] && abc[i+1]== sub[1] && abc[i+2]==sub[2])
{
count++;
for(int j=i;j<=len-2;j++)
{
temp[j]=abc[j+3];
i+=3;
len-=3;
goto tt;
}
}
temp[v++]=abc[i];

tt:
i++;
}

return 0;
}
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:39PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month