Thread: Creating String Functions

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Creating String Functions

    I was wondering if there was any possible way to create a string function like string.length() or string.find(). This what I have so far, but I'd like to have it be string.rplce(find,replace) instead of string = rplce(string,find,replace).

    Code:
    #include <string.h>
    #include <iostream>
    using namespace std;
    
    string rplce(string, string, string);
    
    string rplce(string str, string find, string replace) {
         int pos = str.find(find);
         while(pos!=string::npos) {
              str.replace(pos, find.size(), replace);
              pos = str.find(find, pos);
         }
         return str;
    }
    
    int main() {
         string exmpl = "This is an example string";
         exmpl = rplce(exmpl, "is", "at");
         cout << exmpl << endl;
         system("PAUSE");
         return 0;
    }
    That outputs "That at an example string" whichs is what I want it to do. But I want to do this instead:

    Code:
    ...
         exmpl.rplce("is","at");
    ...
    Any help is greatly appreciated. Thanks!!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can't add to the string class's member interface. You can, however, make the str variable a reference, so that it is updated directly and you can call it like this:
    Code:
    rplce(exmpl, "is","at");
    That is a better way to add functionality to an existing class anyway.

    BTW, your include should be <string>, not <string.h>. Those are two completely different headers.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Thanks for the information. When I use rplce(exmpl, "is", "at");, it doesnt update the exmpl variable. I tried using different combinations of &exmpl and string &str and string* str. I cant figure out how to replace the variable directly like you said. Could you post more code that just that? Thanks.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    void rplce(string& str, string find, string replace)
    {
    //...
    }
    It's up to you to decide if you need it to return anything.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you want the real class syntax, try inheriting from std::string:
    Code:
    class MyString : public std::string
    {
    	public:
    		MyString(const std::string& String) : std::string(String) { }
    		int MyLength();
    };
    
    int MyString::MyLength()
    {
    	return size() * 2; // :)
    }
    
    int main()
    {
    	MyString s = "Hello";
    	std::cout << s.MyLength(); //Outputs 5 * 2 = 10
    
    	return 0;
    }
    Fixing all constructors may require some work though!
    Last edited by Magos; 04-30-2006 at 04:54 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Is that really a good idea though? For some reason I thought that there was something wrong with inheriting from standard library classes, though I don't remember what.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    EDIT: I had a typo in my code...Thanks for you help it works now.

    Magos, thanks for that code/information, but I'd prefer to keep the existing string class. I'm not to worried about being able to have my own function thing. I didnt think it would be that difficult/time-consuming.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> try inheriting from std::string
    As JaWiB mentioned, std::string and the other container classes are not meant to be inherited from. If anybody tries to use the class polymorphically, they could easily get undefined behavior. Even if they don't, a nonmember, nonfriend function is still preferred over creating an entirely new class to add functionality.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Thanks for the information. I think I'll just stick to my function than worrying about classes (since I dont know much about classes).

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM