First, some notes on your code:
<cstring> (i.e. <string.h>) is useless, since you don't use anything from the <cstring> header in your code.
Since string is an input read-only parameter, you can pass it by const &.
Since you want an array of chars, you can either return a vector<char> (or even a string...).
To solve your problem, you can simply iterate through the characters of the input string, and if they are alphabetical and numeric characters (i.e. no space, no comma, etc.), you can add them to the output result vector of chars.
You may want to consider the following sample commented code:
#include <ctype.h> // For isalnum()
#include <iostream> // For console output
#include <string> // For std::string
#include <vector> // For std::vector
using namespace std;
vector<char> split(const string& str)
{
vector<char> result;
// For each character in the string
for (char ch : str)
{
// Copy only alphabetical characters and numeric digits
if (isalnum(ch))
{
result.push_back(ch);
}
}
return result;
}
int main()
{
vector<char> result = split("How are you doing?");
cout << "{ ";
for (char ch : result)
{
cout << "'" << ch << "' ";
}
cout << "}" << endl;
}
Output:
{ 'H' 'o' 'w' 'a' 'r' 'e' 'y' 'o' 'u' 'd' 'o' 'i' 'n' 'g' }
If you like a more "functional" style, you can use the std::copy_if() algorithm, with some code like this:
#include <algorithm> // For std::copy_if()
#include <iterator> // For std::back_inserter
....
const string str = "How are you doing?";
vector<char> result;
copy_if(
str.begin(), str.end(), // copy source
back_inserter(result), // copy destination
[](char ch) { return isalnum(ch); } // when to copy the character
);
str[i]instead ofstr.substr(i,i+1)to get achar.