Mega String Manipulation

This is a discussion on Mega String Manipulation within the C++ Programming forums, part of the General Programming Boards category; How would I be able to manipulate a string so that 1. I can change character x to another character ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Mega String Manipulation

    How would I be able to manipulate a string so that

    1. I can change character x to another character

    2. I can add a certain character in between two characters

    3. I can change all the characters of a certain type to another character

    Code would be useful please...

    Thanks,

    Bumfluff

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Or even easier...

    How do I assign the contents of a string to a character array which has the same length as the string.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    http://www.cppreference.com/cppstring/index.html

    Look at the functions in there. They should answer your first few questions.
    Code:
    std::string str1 = "Hello World!";
    char str2[str1.size() + 1]; 
    strcpy(str2, str1.c_str());
    Sent from my iPad®

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well I worked out an easier way to do most of it with an array...but I could only get my array = to the string with extra strange characters on the end.

    Will try your code...

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It works

    thank you

  6. #6
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,101
    1. Make sure that string is not const and call the assignment operator after you index the character. C-strings are easy, just use memset().
    PHP Code:
    std::string brand("Jello Kitty");
    brand[0] = 'H';   // "Hello Kitty"
    char cstrbrand[13] = brand.c_str();
    memset(cstrbrand'J'1);   // "Jello Kitty" again 
    2. Use insert() on the string object.

    3. I'm not sure I understand, but I suppose you could just use the replace function if you know where to start. If not you have to find() it first.
    PHP Code:
    std::string msg("My name is George W. Bush and I approve this message!");
    msg.replace(1114"citizen"); 
    Crap, a bit slow huh? At least the code is pretty.
    Last edited by whiteflags; 05-01-2006 at 02:30 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21