|
-
Apr 19th, 2001, 03:46 PM
#1
Thread Starter
Lively Member
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"?
-
Apr 19th, 2001, 03:54 PM
#2
Frenzied Member
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...
-
Apr 19th, 2001, 03:56 PM
#3
Thread Starter
Lively Member
I'd love to see it CyberCarsten. Did I mention this is a console app. Not sure if that mattters.
-
Apr 19th, 2001, 03:58 PM
#4
Frenzied Member
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.
-
Apr 19th, 2001, 04:05 PM
#5
Thread Starter
Lively Member
What would I need to include to make that sample of yours work?
-
Apr 19th, 2001, 04:07 PM
#6
Frenzied Member
iostream.h , but I'm not sure if it works....I'll check the CD that came with the book just in case...
-
Apr 19th, 2001, 04:11 PM
#7
Frenzied Member
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;
}
-
Apr 19th, 2001, 04:19 PM
#8
Thread Starter
Lively Member
I don't know enough about it to figure out why it crashes. I'll keep looking and learning though.
-
Apr 19th, 2001, 05:10 PM
#9
Monday Morning Lunatic
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
-
Apr 19th, 2001, 06:42 PM
#10
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");
-
Apr 20th, 2001, 08:28 AM
#11
Thread Starter
Lively Member
Parksie, I couldn't get the StringFind function to work in your code.
-
Apr 20th, 2001, 09:27 AM
#12
Monday Morning Lunatic
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
-
Apr 20th, 2001, 10:38 AM
#13
Thread Starter
Lively Member
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.
-
Apr 20th, 2001, 12:08 PM
#14
Monday Morning Lunatic
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
-
Apr 20th, 2001, 12:13 PM
#15
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|