1

I need help removing spaces and special characters from an char array.The problem I keep running into is that the erase function only works on string datatypes, if I'm not mistaken. The assignment calls for a char array not a string so I cant just convert it or have a string variable. I've tried looking it up but everything kind of just suggests to convert it to a string or start off as a string, which I can't do. I'm new to programming and pointers are a little weird to me so if the answer is obvious I am sorry.

    #include "stdafx.h"
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <stdlib.h>
    #include <ctype.h>
    using namespace std;
        
           int main()
            {
                const int SIZE = 80;
                char str[SIZE];
                char* strPtr;
                int strlength;
                int j = 0;
                int front = 0, back; int flag = 1;
                
                strPtr = str;
            
                cout << "This program checks for palidromes." << endl;
                cout << "Please enter a phrase." << endl;
                cout << endl;
                cin.getline(str, SIZE);
            
                //Get the length of string str
            
                strlength = strlen(str);
            
                //Convert all the characters in the string str to uppercase
                for (int i = 0; i < strlength; i++)
                {
                    if (!isupper(str[i]))
                        str[i] = toupper(str[i]);
                }   
            
                //Remove all the special characters except letters a - z
                for (int i = 0; i < strlength; i++)
            
                    if (!isalpha(str[i])||(!isalnum(str[i])))
                    {
                        str.erase(str[i]); // need help here. Not sure how to go about it.
                    }
            
                //str[j] = '\0';
                
                return 0;
            }

2 Answers 2

4
char* tail = std::remove_if(str, str + strlength,
    [](char c){ return !isalpha(c)||(!isalnum(c)); });
strlength = tail - str;
*tail = 0;
Sign up to request clarification or add additional context in comments.

1 Comment

I actually like the use of lambdas here. +1
3

The other answer correctly points you to std::remove_if, but I suspect that you really want to understand how to implement the removal yourself, instead of dumping the problem on the C++ library. Also your own attempt at this converts all remaining characters to uppercase, which the other answer does not do.

This is actually simpler than you might think. The relevant bits are as follows:

char *p=str, *q=str;

while (*p)
{
    if (isalpha(*p))
         *q++=toupper(*p);

    ++p;
}

*q='\0';

For starters, there's no need to compute strlen() of this string. Simply iterating a pointer from the beginning of the string to the end, until it's pointing at the terminating '\0' is sufficient.

That would be the p pointer. p starts at the beginning of the string, and each iteration of the while loop increments it, with the while loop stopping at the \0.

Then, if p's character is alphabetic, it gets copied to the q pointer, converting it to uppercase, and advancing the q pointer too.

As such, only alphabetic characters get copied to q, everything else gets skipped.

q starts off pointing to the same buffer as p, the buffer with the string. If the string begins with alphabetic characters, the only thing that will happen is that each character gets copied on top of itself, with both p and q advancing together. As soon as p sees a non-alphabetic character, it continues to advance, but q is "left behind", and will continue to accumulate only any remaining alphabetic characters. And when everything is done, the '\0' gets written to q, terminating the new uppercase-only string.

The only real difficult part of this is understanding why everything must occur exactly in the sequence; i.e. the alphabetic character must be copied before p gets incremented, etc... Talk to your rubber duck, if you do not understand why, your rubber duck will explain it to you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.