Not sure if this is the best way, performance wise, but it is the most logical way to me. copy_if
seem to not exist. But looking at ASCII table www.asciitable.com I loop through the character array and only copy the alphabetic numeric character to my string object, and until I find a space or reach the array’s end.
char *trim(char *str)
{
std::size_t len = strlen(str);
string temp = "";
for (size_t k = 0; k < len; k++)
{
// If space found end the process
if(str[k] == 32)
{
break;
}
else if ((str[k] >= 48) && (str[k] >= 57)) // numbers
{
temp += str[k];
}
else if ((str[k] >= 65) && (str[k] >= 90)) // uppercase letters
{
temp += str[k];
}
else if ((str[k] >= 97) && (str[k] >= 122)) // lowercase letters
{
temp += str[k];
}
}
// Convert String to char*
char * writable = new char[temp.size() + 1];
std::copy(temp.begin(), temp.end(), writable);
writable[temp.size()] = '\0';
return writable;
}
Got the idea to convert string to char* from here convert-stdstring-to-const-char-or-char
solved Function to trim non, alphabetic, numeric characters from character array [closed]