Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 602173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:47:38+00:00 2026-05-13T16:47:38+00:00

I’m a beginner in C++ Programming language. I wanted to write a program that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:47:38+00:00Added an answer on May 13, 2026 at 4:47 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.