Results 1 to 15 of 15

Thread: Very Simple string replace

  1. #1

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    So how do you replace part of a string in C++.

    Say I have a string:

    "The cat is black."

    And I want to replace "cat" with "dog"?

  2. #2
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    I have just read about that, I think.
    It used pointers to do it.
    I think I have an example that shows this.
    Let me know of you want it...
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  3. #3

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    I'd love to see it CyberCarsten. Did I mention this is a console app. Not sure if that mattters.

  4. #4
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Here it is:

    Pointer at strings:

    Code:
    // A string
    char *MyString ="hello world";
    
    // Another *chat
    char = *CharPtr;
    
    // Change the first character
    CharPtr = MyString
    *CharPtr = 'j';
    
    // Move on to next character
    
    // Do so by incrementing the pointer
    
    CharPtr++;
    
    // Now change the second character
    *CharPtr ='o'
    
    // The string is now changed to "jollo world".
    
    cout << MyString;
    Hope you can use this.
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  5. #5

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    What would I need to include to make that sample of yours work?

  6. #6
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    iostream.h , but I'm not sure if it works....I'll check the CD that came with the book just in case...
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  7. #7
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    This code will compile, but then it crashes...

    Code:
    #include <iostream.h>
    
    void main()
    {
    	
    // A string
    char *MyString ="hello world";
    
    // Another *chat
    char  *CharPtr;
    
    // Change the first character
    CharPtr = MyString;
    *CharPtr = 'j';
    
    // Move on to next character
    
    // Do so by incrementing the pointer
    
    CharPtr++;
    
    // Now change the second character
    *CharPtr ='o';
    
    // The string is now changed to "jollo world".
    
    cout << MyString;
    }
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  8. #8

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    I don't know enough about it to figure out why it crashes. I'll keep looking and learning though.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If I remember correctly...
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool Replace(string &sLine, string sReplaceWhat, string sReplaceWith) {
        uint nPos = 0;
        uint i = 0;
        while (StringFind(sLine, sReplaceWhat, nPos)) {
            if(i > 100) break; // Something's gone wrong (sReplaceWhat ~= sReplaceWith)
            sLine = sLine.replace(nPos, sReplaceWhat.length(), sReplaceWith);
            i++;
        }
        if(i) return true; return false;
    }
    
    int main() {
        string sStr = "Hello this is a dog";
    
        Replace(sStr, "dog", "albatross");
    
        cout << sStr << endl;
    
        return 0;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    denniswrenn
    Guest
    Originally posted by CyberCarsten
    This code will compile, but then it crashes...

    Code:
    #include <iostream.h>
    
    void main()
    {
    	
    // A string
    char *MyString ="hello world";
    
    // Another *chat
    char  *CharPtr;
    
    // Change the first character
    CharPtr = MyString;
    *CharPtr = 'j';
    
    // Move on to next character
    
    // Do so by incrementing the pointer
    
    CharPtr++;
    
    // Now change the second character
    *CharPtr ='o';
    
    // The string is now changed to "jollo world".
    
    cout << MyString;
    }
    The reason it doesn't work is because you need to assign values to character pointers using strcpy and strcat, you should also allocate enough memory for the string to fit in, IE:

    Code:
    char *pcStr = new char[12];
    strcpy(*pcStr, "hello world");

  11. #11

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    Parksie, I couldn't get the StringFind function to work in your code.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Bugger...that's because I forgot to post it
    Code:
    bool StringFind(string sSearchIn, string sSearchFor, uint &nPos) {
    	uint nTemp = sSearchIn.find(sSearchFor);
    	if(nTemp < sSearchIn.length()) {
    		nPos = nTemp;
    		return true;
    	}
    
    	return false;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    It was looking good, but I was having problems with the cout, so I just tried this simple experiment.
    Code:
    #include <iostream.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string sStr = "Hello this is a dog";
    	cout << sStr;
    	return 0;
    }
    And couldn't get it to work.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You need to use the Standard Library: iostream not iostream.h
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    You are so smart parksie.

    Thanks again!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width