Thread: Replacing characters

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Replacing characters

    Hey,

    I would like to replace every character in a string that is not a-z A-Z 0-9 or ' with a space. Currently I am trying to approach it this way:

    Code:
    replace(ss.begin(), ss.end(), '!', ' ');
    Which would require dozens of those statements, one for each character that I don't want. Is there anyway to have the program check the string and convert any character that is not a-zA-Z' with a space, instead of writing individual replace statements?
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Use transform. Write a function that takes a character and returns that character if it is alphanumeric, or ' ' if it is not.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    So something like this?

    Code:
    #include <ctype.h>
    
    char checkVal(char x)
    {
      if(isalnum(x))
        return x;
      else
        return " ";
    }
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> return " "
    That should be return ' ' (use single quotes for characters).

    You need to call that in a loop on each character in your string. Using the transform function from <algorithm> can help you do that in one line of code.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    So if I wanted to convert every character that is not a letter, ', or a space to a space, the code would be:

    Code:
    string modifyWord(string ss)
    {
    
        for(int xx = 0; xx < (int)ss.length(); xx++)
        {
            ss[xx] = tolower(ss[xx]);
            if(!isalpha(ss[xx]) && ss[xx] != ' ' && ss[xx] != "'")
                ss[xx] = ' ';
        }
    
        return ss;
    }
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Using the transform function as suggested:
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <cctype>
    
    char checkVal(char x)
    {
        if(std::isalnum(x))
            return x;
        else
            return ' ';
    }
    
    int main()
    {
        std::string ss("ab$^&cdefg!@123");
    
        std::cout << ss << std::endl;
    
        std::transform(ss.begin(),ss.end(),ss.begin(),checkVal);
    
        std::cout << ss << std::endl;
    
        return 0;
    }
    Outputs:
    Code:
    ab$^&cdefg!@123
    ab   cdefg  123
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why didn't you use checkVal in your loop? Instead you used different code that probably doesn't compile and doesn't do the same thing.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ok, thanks. When I was trying it a different way however I came across a compiler error that intrigued me. The error was this:

    Code:
    18 main.cpp ISO C++ forbids comparison between pointer and integer
    And the code:

    Code:
    for(int xx = 0; xx < (int)ss.length(); xx++)
        {
            ss[xx] = tolower(ss[xx]);
            if(!isalpha(ss[xx]) && ss[xx] != "'")
                ss[xx] = ' ';
        }
    Just the statement if(!isalpha(ss[xx])) worked, but when I added && ss[xx] != " ' " (apostrophe), that error arose.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you need to put the apostrophe character between character marks you need to escape it.

    I wouldn't take special measures to prevent a space being replaced with a space.

    Code:
            if(!isalpha(ss[xx]) && ss[xx] != ' ' && ss[xx] != '\'')
                ss[xx] = ' ';
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Replacing Characters
    By carrotcake1029 in forum C Programming
    Replies: 3
    Last Post: 04-28-2008, 01:08 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM