C++ Find and Replace with strtok











up vote
-1
down vote

favorite












I am trying to do a find and replace in c++ using strtok.
Basically I have to replace every occurrence of string b in string a with string c. I have this code:



#include<string.h>
#include<iostream>
using namespace std;
char a[255], b[255], c[255];
char *p;
int n;
int main(){
cin.get(a, 255); cin.get();
cin.get(b, 255); cin.get();
cin.get(c, 255); cin.get();
p=strtok(a, b);
while(p!=NULL){
n=strlen(p);
p=strtok(NULL, " ");
}
cout<<a;
return 0;
}


If string a is:"word", b is "or" and c is "wjik", then string a should become "wwjikd".

How can I do this with the str functions?










share|improve this question
























  • Does it work? What's broken about it?
    – nicomp
    Nov 5 at 19:46










  • It's incomplete. It splits a by b(the find part) but I have no idea how to do the replace part.
    – Wolfuryo
    Nov 5 at 19:46






  • 1




    I'm not sure why you need to use C functions in a C++ program, but OK. Anyway, you're off to a good start, why are you stuck at this point? Maybe you just need to take a break.
    – Mr Lister
    Nov 5 at 19:46








  • 1




    The simplest algorithm for a replace is: use an output buffer. Make use of strstr to search for the search argument. Copy the first part of the original string to the buffer, followed by the replacement string; then continue until the search argument is no longer found in the remainder of the original string. Return the buffer. This will save you the byte shuffling required for an in-place replacement, which is much harder top debug.
    – Mr Lister
    Nov 5 at 20:05








  • 1




    Start by reading up on strstr. Unless you are going full C++ and using std::string, which has wonderful search tools built right in, strstr's the best option.
    – user4581301
    Nov 5 at 20:14

















up vote
-1
down vote

favorite












I am trying to do a find and replace in c++ using strtok.
Basically I have to replace every occurrence of string b in string a with string c. I have this code:



#include<string.h>
#include<iostream>
using namespace std;
char a[255], b[255], c[255];
char *p;
int n;
int main(){
cin.get(a, 255); cin.get();
cin.get(b, 255); cin.get();
cin.get(c, 255); cin.get();
p=strtok(a, b);
while(p!=NULL){
n=strlen(p);
p=strtok(NULL, " ");
}
cout<<a;
return 0;
}


If string a is:"word", b is "or" and c is "wjik", then string a should become "wwjikd".

How can I do this with the str functions?










share|improve this question
























  • Does it work? What's broken about it?
    – nicomp
    Nov 5 at 19:46










  • It's incomplete. It splits a by b(the find part) but I have no idea how to do the replace part.
    – Wolfuryo
    Nov 5 at 19:46






  • 1




    I'm not sure why you need to use C functions in a C++ program, but OK. Anyway, you're off to a good start, why are you stuck at this point? Maybe you just need to take a break.
    – Mr Lister
    Nov 5 at 19:46








  • 1




    The simplest algorithm for a replace is: use an output buffer. Make use of strstr to search for the search argument. Copy the first part of the original string to the buffer, followed by the replacement string; then continue until the search argument is no longer found in the remainder of the original string. Return the buffer. This will save you the byte shuffling required for an in-place replacement, which is much harder top debug.
    – Mr Lister
    Nov 5 at 20:05








  • 1




    Start by reading up on strstr. Unless you are going full C++ and using std::string, which has wonderful search tools built right in, strstr's the best option.
    – user4581301
    Nov 5 at 20:14















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to do a find and replace in c++ using strtok.
Basically I have to replace every occurrence of string b in string a with string c. I have this code:



#include<string.h>
#include<iostream>
using namespace std;
char a[255], b[255], c[255];
char *p;
int n;
int main(){
cin.get(a, 255); cin.get();
cin.get(b, 255); cin.get();
cin.get(c, 255); cin.get();
p=strtok(a, b);
while(p!=NULL){
n=strlen(p);
p=strtok(NULL, " ");
}
cout<<a;
return 0;
}


If string a is:"word", b is "or" and c is "wjik", then string a should become "wwjikd".

How can I do this with the str functions?










share|improve this question















I am trying to do a find and replace in c++ using strtok.
Basically I have to replace every occurrence of string b in string a with string c. I have this code:



#include<string.h>
#include<iostream>
using namespace std;
char a[255], b[255], c[255];
char *p;
int n;
int main(){
cin.get(a, 255); cin.get();
cin.get(b, 255); cin.get();
cin.get(c, 255); cin.get();
p=strtok(a, b);
while(p!=NULL){
n=strlen(p);
p=strtok(NULL, " ");
}
cout<<a;
return 0;
}


If string a is:"word", b is "or" and c is "wjik", then string a should become "wwjikd".

How can I do this with the str functions?







c++ strtok find-replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 19:43









Mr Lister

34k1072113




34k1072113










asked Nov 5 at 19:39









Wolfuryo

60118




60118












  • Does it work? What's broken about it?
    – nicomp
    Nov 5 at 19:46










  • It's incomplete. It splits a by b(the find part) but I have no idea how to do the replace part.
    – Wolfuryo
    Nov 5 at 19:46






  • 1




    I'm not sure why you need to use C functions in a C++ program, but OK. Anyway, you're off to a good start, why are you stuck at this point? Maybe you just need to take a break.
    – Mr Lister
    Nov 5 at 19:46








  • 1




    The simplest algorithm for a replace is: use an output buffer. Make use of strstr to search for the search argument. Copy the first part of the original string to the buffer, followed by the replacement string; then continue until the search argument is no longer found in the remainder of the original string. Return the buffer. This will save you the byte shuffling required for an in-place replacement, which is much harder top debug.
    – Mr Lister
    Nov 5 at 20:05








  • 1




    Start by reading up on strstr. Unless you are going full C++ and using std::string, which has wonderful search tools built right in, strstr's the best option.
    – user4581301
    Nov 5 at 20:14




















  • Does it work? What's broken about it?
    – nicomp
    Nov 5 at 19:46










  • It's incomplete. It splits a by b(the find part) but I have no idea how to do the replace part.
    – Wolfuryo
    Nov 5 at 19:46






  • 1




    I'm not sure why you need to use C functions in a C++ program, but OK. Anyway, you're off to a good start, why are you stuck at this point? Maybe you just need to take a break.
    – Mr Lister
    Nov 5 at 19:46








  • 1




    The simplest algorithm for a replace is: use an output buffer. Make use of strstr to search for the search argument. Copy the first part of the original string to the buffer, followed by the replacement string; then continue until the search argument is no longer found in the remainder of the original string. Return the buffer. This will save you the byte shuffling required for an in-place replacement, which is much harder top debug.
    – Mr Lister
    Nov 5 at 20:05








  • 1




    Start by reading up on strstr. Unless you are going full C++ and using std::string, which has wonderful search tools built right in, strstr's the best option.
    – user4581301
    Nov 5 at 20:14


















Does it work? What's broken about it?
– nicomp
Nov 5 at 19:46




Does it work? What's broken about it?
– nicomp
Nov 5 at 19:46












It's incomplete. It splits a by b(the find part) but I have no idea how to do the replace part.
– Wolfuryo
Nov 5 at 19:46




It's incomplete. It splits a by b(the find part) but I have no idea how to do the replace part.
– Wolfuryo
Nov 5 at 19:46




1




1




I'm not sure why you need to use C functions in a C++ program, but OK. Anyway, you're off to a good start, why are you stuck at this point? Maybe you just need to take a break.
– Mr Lister
Nov 5 at 19:46






I'm not sure why you need to use C functions in a C++ program, but OK. Anyway, you're off to a good start, why are you stuck at this point? Maybe you just need to take a break.
– Mr Lister
Nov 5 at 19:46






1




1




The simplest algorithm for a replace is: use an output buffer. Make use of strstr to search for the search argument. Copy the first part of the original string to the buffer, followed by the replacement string; then continue until the search argument is no longer found in the remainder of the original string. Return the buffer. This will save you the byte shuffling required for an in-place replacement, which is much harder top debug.
– Mr Lister
Nov 5 at 20:05






The simplest algorithm for a replace is: use an output buffer. Make use of strstr to search for the search argument. Copy the first part of the original string to the buffer, followed by the replacement string; then continue until the search argument is no longer found in the remainder of the original string. Return the buffer. This will save you the byte shuffling required for an in-place replacement, which is much harder top debug.
– Mr Lister
Nov 5 at 20:05






1




1




Start by reading up on strstr. Unless you are going full C++ and using std::string, which has wonderful search tools built right in, strstr's the best option.
– user4581301
Nov 5 at 20:14






Start by reading up on strstr. Unless you are going full C++ and using std::string, which has wonderful search tools built right in, strstr's the best option.
– user4581301
Nov 5 at 20:14



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53161099%2fc-find-and-replace-with-strtok%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53161099%2fc-find-and-replace-with-strtok%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini