|
 |
|
 |
10-27-2005, 09:04 PM
|
#16 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Quote:
|
Originally Posted by redhead
Perhaps something like:
Code:
int remove_string(char* in_str, string remove_str)
{
std::string res_str(in_str);
int i;
if(!strstr(in_str, remove_str.c_str()))
return 0;
while(string::npos != (i=res_str.find(remove_str)))
res_str.replace(i, remove_str.length(), remove_str);
return 1;
}
|
I think I've almost figured out your code, as is it won't give me any results, I'll show you what I have with my full-code, but first I want to understand fully what is happening here.
Quote:
|
Originally Posted by redhead
Code:
std::string res_str(in_str);
We're just creating another instance of the in_str to use within the function, correct?
|
Quote:
|
Originally Posted by redhead
Code:
if(!strstr(in_str, remove_str.c_str())) return 0;
|
This one beats the hell outta me, i've never seen a strstr call before, from what I can put together it looks like if we haven't already made remove_str into a c-style array, it will be done here.
Quote:
|
Originally Posted by redhead
Code:
while(string::npos != (i=res_str.find(remove_str)))
res_str.replace(i, remove_str.length(), remove_str);
return 1;
|
Lastly, this little chunk has been making me pull out some hairs. from what I can gather, the string::npos will search for a string inside a string, and place the address of the starting character to i. After which we will begin by removing the portion of the string starting at i, until we have removed enough to have matched the length of remove_str. We should now have a new string that has rid us of the word we didn't want, and return a 1 on success. I think it's slowly making sense to me, but my code will not perform correctly on execution still, it will compile and execute however. any thoughts?
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int remove_string(char* in_str, string remove_str);
int main()
{
string in_str;
string remove_str;
char* char_str;
cout << "Input string to process: ";
cout.flush();
getline(cin, in_str);
cout << "Input word to be removed: ";
cin >> remove_str;
char_str = new char(in_str.size()); //dynamically allocate for the size of in_str after input
if(!char_str)
{
cout << "Error allocating space for input string." << endl;
return -1;
}
if(!memcpy(char_str, in_str.c_str(), in_str.size()))
{
cout << "Error copying string to char array." << endl;
return -1;
}
if(!remove_string(char_str, remove_str)) //if both arguments aren't valid (remove_str doesn't match anything in in_str)
{
cout << "Unable to remove the string" << endl;
return -1;
}
remove_string(char_str, remove_str);
cout << "After eliminating \"" << remove_str <<"\" the string is: " << in_str << endl;
system("pause");
return 0;
}
int remove_string(char* in_str, string remove_str)
{
string res_str(in_str);
int i;
if(!strstr(in_str, remove_str.c_str()))
return 0;
while(string::npos != (i=res_str.find(remove_str)))
res_str.replace(i, remove_str.length(), remove_str);
return 1;
}
|
|
|
10-27-2005, 09:39 PM
|
#17 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Code:
if(!strstr(in_str, remove_str.c_str())) return 0;
strstr() returns the location of where the string is to be found, if it can't be found it returns NULL, thus this will return 0 when theres no occurence of the string to be removed.
Here is a working example with the code you're using.. with a few alterations
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int remove_string(char* in_str, string remove_str);
int main()
{
string in_str;
string remove_str;
char* char_str;
cout << "Input string to process: ";
cout.flush();
getline(cin, in_str);
cout << "Input word to be removed: ";
cin >> remove_str;
char_str = new char(in_str.size()); //dynamically allocate for the size of in_str after input
if(!char_str)
{
cout << "Error allocating space for input string." << endl;
return -1;
}
if(!memcpy(char_str, in_str.c_str(), in_str.size()))
{
cout << "Error copying string to char array." << endl;
return -1;
}
if(!remove_string(char_str, remove_str)) //if both arguments aren't valid (remove_str doesn't match anything in in_str)
{
cout << "Unable to remove the string" << endl;
return -1;
}
in_str=char_str;
cout << "After eliminating \"" << remove_str <<"\" the string is: " << in_str << endl;
system("pause");
return 0;
}
int remove_string(char* in_str, string remove_str)
{
string res_str(in_str);
int i;
if(!strstr(in_str, remove_str.c_str()))
return 0;
while(string::npos != (i=res_str.find(remove_str)))
res_str.replace(i, remove_str.length(), "");
memcpy(in_str, res_str.c_str(), strlen(in_str));
return 1;
}
|
|
|
10-27-2005, 10:03 PM
|
#18 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
thanks for the help redhead, it's helping me understand now a bit better the way dynamic memory works, and pointers. I'm going to go over this and try to expand on it on my own time.
|
|
|
10-27-2005, 10:10 PM
|
#19 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
That was, what the intention with this small exploration intentionaly was.. A way to explore some aspects of C/C++ without having some deadline hanging over your head, as a frustrating element.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 07:15 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|