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?
c++ strtok find-replace
|
show 6 more comments
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?
c++ strtok find-replace
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 ofstrstr
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 onstrstr
. Unless you are going full C++ and usingstd::string
, which has wonderful search tools built right in,strstr
's the best option.
– user4581301
Nov 5 at 20:14
|
show 6 more comments
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?
c++ strtok find-replace
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
c++ strtok find-replace
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 ofstrstr
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 onstrstr
. Unless you are going full C++ and usingstd::string
, which has wonderful search tools built right in,strstr
's the best option.
– user4581301
Nov 5 at 20:14
|
show 6 more comments
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 ofstrstr
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 onstrstr
. Unless you are going full C++ and usingstd::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
|
show 6 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 usingstd::string
, which has wonderful search tools built right in,strstr
's the best option.– user4581301
Nov 5 at 20:14