cStrings Remove non-alpha/non-space character - C++

3.1k views Asked by At

So I need to create a function that removes all non-letter and non-space character characters from a string of characters (c-string).

For example: "I'm upset that on Nov. 15th, 2014, my 2 brand-new BMW 750Lis were stolen!!" should be turned into "im upset that on nov th my brandnew bmw lis were stolen".

documentCopy[201] = "I'm upset that on Nov. 15th, 2014, my 2 brand-new BMW 750Lis were stolen!!";

for (int i = 0; documentCopy[i] != '\0'; i++)
{
    if (!isalpha(documentCopy[i]) && !isspace(documentCopy[i]))
    {

        for (int k = i; documentCopy[k] != '\0'; k++)
        {
            documentCopy[k] = documentCopy[k+1];

        }
    }
}
cout << documentCopy << endl;

Unfortunately the output is "Im upset that on Nov 5th 04 my brandnew BMW 5Lis were stolen!"

Please help!

5

There are 5 answers

0
Himanshu On BEST ANSWER

After One Line After this loop

for (int k = i; documentCopy[k] != '\0'; k++)
{
    documentCopy[k] = documentCopy[k+1];
}
i--;  //Add This line in your Code.

This will work.

for example
if you are checking a[0] and shifting a[0] = a[1]
So you need to check a[0] again because now it is holding value of a[1] now, so need to decrease the index value.

0
Tony Delroy On

When you discard a character you copy the next character over it, but then you want to consider whether that next character should also be discarded. To do that, you need to avoid the ++i in such circumstances, i.e.:

for (int i = 0; documentCopy[i] != '\0'; )
{
    if (!isalpha(documentCopy[i]) && !isspace(documentCopy[i]))
    {

        for (int k = i; documentCopy[k] != '\0'; k++)
        {
            documentCopy[k] = documentCopy[k+1];
        }
    }
    else
        ++i;
}
0
Alexander V On
void Voila()
{
    char documentCopy[] = "I'm upset that on Nov. 15th, 2014, my 2 brand-new BMW 750Lis were stolen!!";
    char* psrc = documentCopy;
    char* pdst = documentCopy;

    for( ; *psrc; ++psrc)
    {
        if (isalpha(*psrc) || isspace(*psrc))
            *pdst++ = *psrc;
    }
    *pdst = 0;
    cout << documentCopy << endl;
}
0
cpx On

The following code ignores all the spaces and removes all non-characters:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {

    std::string str = "I'm upset that on Nov. 15th, 2014, my 2 brand-new BMW 750Lis were stolen!!";
    str.erase(std::remove_if(str.begin(), str.end(), [](const unsigned &c){ return !isspace(c) && !isalpha(c);}), str.end());
    std::cout << str;
    return 0;
}

The output will be:

Im upset that on Nov th my brandnew BMW Lis were stolen

You can also use remove_if on C-strings if you have to:

char cstring[] = "I'm upset that on Nov. 15th, 2014, my 2 brand-new BMW 750Lis were stolen!!";
int count = 0;
std::remove_if(cstring, cstring+sizeof(cstring), [&count](const unsigned char &c)
                                            {
                                                if (!isspace(c) && !isalpha(c))
                                                {
                                                    ++count;
                                                    return true;
                                                }
                                                else
                                                    return false;                               
        });

cstring[sizeof(cstring) - count] = 0;

std::cout << cstring;
0
Gaurav Sehgal On

Once you find a non space and non alpha character, you move the entire string left by one place,after which i is incremented and no longer points to the new character which has arrived at the unwanted character's place.So if there are consecutive unwanted characters(non space and non alpha)your code would not be able to detect it.