3

I'm trying to remove all non alphabet characters from an inputed string in c++ and don't know how to. I know it probably involves ascii numbers because that's what we're learning about. I can't figure out how to remove them. We only learned up to loops and haven't started arrays yet. Not sure what to do.

If the string is Hello 1234 World&*
It would print HelloWorld

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Holden Spencer
  • 31
  • 1
  • 1
  • 2
  • Are you working with C-style `char*` (or `wchar_t*`) strings or C++ STL `std::string` (or `std::wstring`) instances? – Dai Feb 13 '15 at 03:26
  • I think char*. not sure what you're asking. I assume it's what ever is standard. I know I don't use the " std:: " thing. – Holden Spencer Feb 13 '15 at 03:28
  • std::string is the standard string in c++. char* is c not c++. – drescherjm Feb 13 '15 at 03:34
  • I disagree about this question being a duplicate of the linked one. This question is about removing non-alphabet characters, while the other is about non-alphanumeric characters. The top answer here is a good answer to this OP, while the top answer on the linked question is not. – Lorien Brune Sep 28 '17 at 17:52

4 Answers4

13

If you use std::string and STL, you can:

string s("Hello 1234 World&*");
s.erase(remove_if(s.begin(), s.end(), [](char c) { return !isalpha(c); } ), s.end());

http://ideone.com/OIsJmb

Note: If you want to be able to handle strings holding text in just about any language except English, or where programs use a locale other than the default, you can use isalpha(std::locale).

PS: If you use a c-style string such as char *, you can convert it to std::string by its constructor, and convert back by its member function c_str().

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
2

If you're working with C-style strings (e.g. char* str = "foobar") then you can't "remove" characters from a string trivially (as a string is just a sequence of characters stored sequentially in memory - removing a character means copying bytes forward to fill the empty space used by the deleted character.

You'd have to allocate space for a new string and copy characters into it as-needed. The problem is, you have to allocate memory before you fill it, so you'd over-allocate memory unless you do an initial pass to get a count of the number of characters remaining in the string.

Like so:

void BlatentlyObviousHomeworkExercise() {

    char* str = "someString";
    size_t strLength = ... // how `strLength` is set depends on how `str` gets its value, if it's a literal then using the `sizeof` operator is fine, otherwise use `strlen` (assuming it's a null-terminated string).

    size_t finalLength = 0;
    for(size_t i = 0; i < strLength; i++ ) {
        char c = str[i]; // get the ith element of the `str` array.
        if( IsAlphabetical(c) ) finalLength++;
    }

    char* filteredString = new char[ finalLength + 1 ]; // note I use `new[]` instead of `malloc` as this is C++, not C. Use the right idioms :) The +1 is for the null-terminator.
    size_t filteredStringI = 0;
    for(size_t i = 0; i < strLength; i++ ) {
        char c = str[i];
        if( IsAlphabetical(c) ) filteredString[ filteredStringI++ ] = c;
    }
    filteredString[ filteredStringI ] = '\0'; // set the null terminator
}

bool IsAlphabet(char c) { // `IsAlphabet` rather than `IsNonAlphabet` to avoid negatives in function names/behaviors for simplicity
    return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
Dai
  • 141,631
  • 28
  • 261
  • 374
1

I do not want to spoil the solution so I will not type out the code, only describe the solution. For your problem think of iterating through your string. Start with that. Then you need to decide if the currently selected character is part of the alphabet or not. You can do this numerous different ways. Checking ASCII values? Comparing against a string of the alphabet? Once you decide if it is a letter, then you need to rebuild the new string with that letter plus the valid letters before and after that you found or will find. Finally you need to display your new string.

Mr. Foots
  • 330
  • 2
  • 11
0

If you look at an ascii table, you can see that A-Z is between 65-90 and a-z is between 97-122.

So, assuming that you only need to remove those characters (not accentuated), and not other characters from other languages for example, not represented in ascii, all you would need to do is loop the string, verify if each char is in these values and remove it.

Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
  • Thank you, what I'm having trouble with is figuring out to make the loop that loops through the string. Especially since I don't know the length of the string. – Holden Spencer Feb 13 '15 at 03:31
  • Take a look here: http://stackoverflow.com/questions/9438209/for-every-character-in-string. If you're using a `char*` instead of `std:string`, you want the 4th option of the accepted answer. – Hugo Sousa Feb 13 '15 at 03:33