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"?
Printable View
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"?
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...
I'd love to see it CyberCarsten. Did I mention this is a console app. Not sure if that mattters.
Here it is:
Pointer at strings:
Hope you can use this.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;
What would I need to include to make that sample of yours work?
iostream.h , but I'm not sure if it works....I'll check the CD that came with the book just in case...
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;
}
I don't know enough about it to figure out why it crashes. I'll keep looking and learning though.
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;
}
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:Quote:
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;
}
Code:char *pcStr = new char[12];
strcpy(*pcStr, "hello world");
Parksie, I couldn't get the StringFind function to work in your code.
Bugger...that's because I forgot to post it :rolleyes:
Code:bool StringFind(string sSearchIn, string sSearchFor, uint &nPos) {
uint nTemp = sSearchIn.find(sSearchFor);
if(nTemp < sSearchIn.length()) {
nPos = nTemp;
return true;
}
return false;
}
It was looking good, but I was having problems with the cout, so I just tried this simple experiment.
And couldn't get it to work.Code:#include <iostream.h>
#include <string>
using namespace std;
int main()
{
string sStr = "Hello this is a dog";
cout << sStr;
return 0;
}
You need to use the Standard Library: iostream not iostream.h :)
You are so smart parksie.
Thanks again!!!