C++ Replace function

Here you can talk about C++ And C# And Other languages programming.

C++ Replace function

Postby Newbie » Fri Nov 10, 2006 8:32 am


:D

#include <stdio.h>
#include <string.h>
#include <stdlib.h

char *str_replace(char * t1, char * t2, char * t6){
char*t4;
char*t5=malloc(0);


while(strstr(t6,t1)){
t4=strstr(t6,t1);
strncpy(t5+strlen(t5),t6,t4-t6);
strcat(t5,t2);
t4+=strlen(t1);
t6=t4;
}
return strcat(t5,t4);
}

//Use
printf("%s",str_replace("hello","test","hello test\n"));
// Returns:Returns the new string with a
// ll occurences of first parameter replace
// d with second parameter.
User avatar
Newbie
imFiles Junior
imFiles Junior
 
Posts: 95
Joined: Fri Oct 07, 2005 2:51 pm

Postby GODFATHER-GM_01 » Fri Nov 10, 2006 8:54 am

guys whats so deaf about c++ and vb6 what dose c++ do?
Image
User avatar
GODFATHER-GM_01
Forum Moderator
Forum Moderator
 
Posts: 221
Joined: Tue Jul 11, 2006 1:02 am
Location: GYPSY MOBB

replace in html

Postby BattleStar-Galactica » Fri Nov 10, 2006 9:46 am

Code: Select all

<HEAD>
<SCRIPT>
function replaceElement()
{
        //The first child of the div is the bold element.   
    var oChild=Div1.children(0);   
    var sInnerHTML = oChild.innerHTML;
    if (oChild.tagName=="B")
    {
        oNewChild=document.createElement("I");
        Div1.replaceChild(oNewChild, oChild);
        oNewChild.innerHTML=sInnerHTML
    }
    else
    {
        oNewChild=document.createElement("B");
        Div1.replaceChild(oNewChild, oChild);
        oNewChild.innerHTML=sInnerHTML      
    }
}
</SCRIPT>
</HEAD>
<BODY>
<DIV ID=Div1 onclick="replaceElement()">
Click anywhere in this sentence to toggle this <B>word</B>
between bold and italic.</DIV>
</BODY>
Big math problem
2 - 1 = 0 = without you I'm nothing
User avatar
BattleStar-Galactica
imFiles Master
imFiles Master
 
Posts: 565
Joined: Tue Sep 20, 2005 12:19 am
Location: safest place to hide

Postby NinjaCoder » Fri Nov 10, 2006 2:15 pm

VINNIE-GM_01 wrote:guys whats so deaf about c++ and vb6 what dose c++ do?


C++ is a much more powerful coding language...u can do a lot more with it....sky is the limit if u really know wut ur doin....but its a lot harder to learn.....i havnt got past VB lol.... :lol:
NinjaCoder
 

Postby Ponies » Fri Nov 10, 2006 5:55 pm

'sky' inst all that big you know.
Ponies
BANNED
BANNED
 
Posts: 5322
Joined: Sun Apr 30, 2006 8:59 am

Postby Ghost » Fri Nov 10, 2006 6:19 pm

but your mom is.
Ghost
 

Postby NinjaCoder » Fri Nov 10, 2006 8:14 pm

lol.....if u keep going up u end up in outer space...lol.... :lol:
NinjaCoder
 

Postby Ponies » Fri Nov 10, 2006 9:47 pm

witch is VERY big. But isn't the sky.
Ponies
BANNED
BANNED
 
Posts: 5322
Joined: Sun Apr 30, 2006 8:59 am

Postby NinjaCoder » Fri Nov 10, 2006 10:08 pm

its an extension of the sky :D
NinjaCoder
 

Re: C++ Replace function

Postby Dexter » Mon Sep 27, 2010 2:02 am

The basic has a function called "Replace" which takes 3 strings and does as i described. I looked at the string::replace but none of its forms can take the 3 stings and do the job. If i use a position and size then surely if the replacer is longer than the replace-ee then thats going to overwrite more than i want it too :/ Plus im not sure how to even get the position or length i need either

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>

using namespace std;


string Replace(string a,string b,string c)
{
int pos = a.find(b);
a.replace(pos, c.length(), c);

return a;
}

int main()

{
std::cout << Replace(" Hello World","Hello","goodbye");
}



This is my attempt but i am getting
goodbyeorld

As its simply overwriting rather than resizing a.

edit:
Ah ok fixed it, i needed to supply the length of b. Now i just need to make it parse the whole string :)

Is this the best way of doing this? by (dman (20) )
Dexter
 
Posts: 3
Joined: Mon Sep 27, 2010 1:54 am
Location: USA

Re: C++ Replace function

Postby String » Mon Sep 27, 2010 12:26 pm

Dexter, do not post data from other forums with out posting a link back to the original forum.
-= Please ask your questions in the forum, not in pm.
String
imFiles Senior
imFiles Senior
 
Posts: 313
Joined: Mon Mar 10, 2008 7:06 am
Location: IDE

Re: C++ Replace function

Postby Chike » Tue Sep 28, 2010 1:36 pm

This looks like a sophisticated spam bot.
Only took it 3 minutes from his hello post to this one.
And of course the n00b has a link in his sig LOL.
Image
Chike
imFiles Master
imFiles Master
 
Posts: 583
Joined: Sun May 13, 2007 6:20 pm

Re: C++ Replace function

Postby waqar_khan » Fri Jan 07, 2011 4:53 am

Hello guys....!
Really nice code shared.Great work.I really appreciate your work.I really like this kind of post which tell us much important information. ...!
waqar_khan
 
Posts: 3
Joined: Fri Jan 07, 2011 4:40 am
Location: USA


Return to C++, C# And Others Programming

Who is online

Users browsing this forum: No registered users and 0 guests