Q:

Invalid conversion char to char* - Copying char in string array to another string array

I'm a beginner in C++ Programming language. I wanted to write a program that take the alphabets in a string array called str, and copy it in a new array called str_alpha.
And the same goes to numbers, the program copies it from str array to str_digit array.
There's my humble code, it might be full of errors and stuff. But this is what I could do now with my very little experience.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

char str[100], str_alpha[100], str_digit[100];

int main()
{
    gets(str);

    for (int i=0 ; str[i] ; i++)
    {
        if (isalpha(str[i]))
        {
            strcpy (str_alpha[i] , str[i]);
        }
        else if (isdigit(str[i]))
        {
            strcpy (str_digit[i] , str[i]);
        }
    }

    cout << "Alpha is " << str_alpha << endl ;
    cout << "Number is : " << str_digit << endl ;

    return 0;
}

And it gives my those errors :
F:\C++Progs\string\main.cpp||In function `int main()':|
F:\C++Progs\string\main.cpp|18|error: invalid conversion from `char' to `char*'|
F:\C++Progs\string\main.cpp|18|error:   initializing argument 1 of `char* strcpy(char*,        const char*)'|
F:\C++Progs\string\main.cpp|18|error: invalid conversion from `char' to `const char*'|
F:\C++Progs\string\main.cpp|18|error:   initializing argument 2 of `char* strcpy(char*,   const char*)'|
F:\C++Progs\string\main.cpp|22|error: invalid conversion from `char' to `char*'|
F:\C++Progs\string\main.cpp|22|error:   initializing argument 1 of `char* strcpy(char*,    const char*)'|
F:\C++Progs\string\main.cpp|22|error: invalid conversion from `char' to `const char*'|
F:\C++Progs\string\main.cpp|22|error:   initializing argument 2 of `char* strcpy(char*, const char*)'|
||=== Build finished: 8 errors, 0 warnings ===|

Help me please.
Thanks in advance.

A:

I'm a beginner in C++ Programming language.

The do not use char* and the likes; use C++ predefined types for this, in your case string:
string str;
cin >> str;

string alpha;
string digit;

for (unsigned int i = 0; i < str.length(); ++i) {
    char chr = str[i];
    if (isalpha(chr))
        alpha.push_back(chr);
    else if (isdigit(chr))
        digit.push_back(chr);
}

Furthermore, strcpy is, as the name says, used to copy strings, not individual chars. You can copy those directly by assigning them. No function call needed.

A:

First of all, strcpy copies C strings (character arrays) not chars. Additionally, the lines strcpy(str_digit[i],str[i]) and strcpy(str_alpha[i], str[i]) would still probably be wrong even if this wasn't the case. Since you haven't initialised the arrays str_digit and str_alpha, you'll get a lot of garbage values while printing them and if any of those garbage values happen to be 0x00, the cout statements will fail to print the whole string. As already mentioned, you really should be using std::string rather than char[] or char*. Having said that, here are corrected versions of your code for both char[] and std::string. 
Using gets is bad practice and you might consider using std::cin instead. And you might want to use an iterator rather than a simple for loop.
//using char[]
#include <iostream>

using namespace std;

int main()
{
    char str[100] , str_alpha[100] , str_digit[100] ;
    int alpha_counter=0, digit_counter=0;

    cin.get(str, 99);

    for (int i=0 ; str[i] ; i++)
    {
        if(isalpha(str[i]))
        {
            str_alpha[alpha_counter] = str[i];
            alpha_counter++;
        }
        else if (isdigit(str[i]))
        {
            str_digit[digit_counter] = str[i];
            digit_counter++;
        }
    } 
    str_alpha[alpha_counter] = 0;
    str_digit[digit_counter] = 0;

    cout << "Alpha is " << str_alpha << endl ;
    cout << "Number is : " << str_digit << endl ;

    return 0;
}

And the version using std::string:
//using std::string
#include <iostream>

using namespace std;

int main()
{
    string str, str_alpha , str_digit;

    cin >> str ;

    for (string::iterator it = str.begin();it<str.end();it++)
    {
        if(isalpha(*it))
        {
            str_alpha += *it;
        }
        else if (isdigit(*it))
        {
            str_digit += *it;
        }
    } 

    cout << "Alpha is " << str_alpha << endl ;
    cout << "Number is : " << str_digit << endl ;

    return 0;
}

Both versions compile without warnings on g++ 4.2.1 with -Wall and -pedantic.

