Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,068 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,798 people online right now. Registration is fast and FREE... Join Now!




how to search and replace in character array

 
Reply to this topicStart new topic

how to search and replace in character array

jansong1
2 Feb, 2008 - 02:32 AM
Post #1

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 2

Hi guys i'm new here.

I am currently stuck with trying to figure out how to replace all the blank spaces (" ") of my character array of size 6 with letter "A"s. Can anyone offer an insight? Didn't used strings. Sorry if this sounds noobish.
User is offlineProfile CardPM
+Quote Post

Bench
RE: How To Search And Replace In Character Array
2 Feb, 2008 - 03:32 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Are you using C or C++? in C++, use the STL replace algorithm
CODE
#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    using namespace std;

    string str = "the quick brown fox jumps over the lazy dog";
    replace( str.begin(), str.end(), ' ', '$' );

    cout << str;
}

For C++, I strongly recommend you use the C++ string type, though you can do the same with char arrays
CODE
    char str[] = "the quick brown fox jumps over the lazy dog";
    replace( str, str+strlen(str), ' ', '$' );
    cout << str;

User is offlineProfile CardPM
+Quote Post

jansong1
RE: How To Search And Replace In Character Array
2 Feb, 2008 - 04:08 AM
Post #3

New D.I.C Head
*

Joined: 2 Feb, 2008
Posts: 2

Hi Bench.

I used the char array method but it does not replace my blanks ' ' with 'A's, instead the code stops there. and those input that have blanks are cut off after the blank.
User is offlineProfile CardPM
+Quote Post

Bench
RE: How To Search And Replace In Character Array
2 Feb, 2008 - 05:18 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(jansong1 @ 2 Feb, 2008 - 12:08 PM) *

Hi Bench.

I used the char array method but it does not replace my blanks ' ' with 'A's, instead the code stops there. and those input that have blanks are cut off after the blank.

Could you paste the code you've tried, and your output?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 10:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month