Thread: String character removal

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    England
    Posts
    9

    String character removal

    Hi,

    I know how to remove certain characters from a string by using something like this:

    Code:
    string str ("Hello world!");
    erase (0, 6);
    That's great if I want to do that manually, but say if someone entered a string, how would I automatically remove every other character they entered? Sorry if this is a poor explanation, but I hope someone can help me.

    -Joe

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Going to guess this is for an assignment. I am also going to guess that your class just taught you about loops. So... think of an implementation with loops that will remove every other character.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    England
    Posts
    9
    Unfortunately my school doesn't teach programming, stuck doing it alone. But thanks for the advice

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Ah, well then here is some reading for you sir to help you in your learning

    For loops:
    for loop - cppreference.com

    While loops:
    while loop - cppreference.com

    do-while loops:
    do-while loop - cppreference.com

    You can use any of these 3 to easily accomplish your goal here. I am not trying to be rude, I'm trying to help you without flat out giving you the answer.

    Also for general reference:
    C++ reference - cppreference.com

  5. #5
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    For a quick and easy strategy, use the STL to your advantage. The <algorithm> header is one of my favorites: remove_if - C++ Reference

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read character by character without string.h?
    By Jimmy King in forum C Programming
    Replies: 2
    Last Post: 10-29-2013, 01:00 AM
  2. Simple Removal of string in string via strtok.
    By Vincent Wouters in forum C Programming
    Replies: 6
    Last Post: 11-11-2011, 01:48 PM
  3. Removal of gotos
    By ninety3gd in forum C++ Programming
    Replies: 14
    Last Post: 03-04-2010, 05:48 AM
  4. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM