Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-27-2005, 09:04 PM   #16 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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;
}
Deliverance is offline   Reply With Quote
Old 10-27-2005, 09:39 PM   #17 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-27-2005, 10:03 PM   #18 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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.
Deliverance is offline   Reply With Quote
Old 10-27-2005, 10:10 PM   #19 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kaat a talking bot in c nvictor Platform/API C++ 10 05-19-2005 02:16 PM
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 01:22 AM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 07:15 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting