-1

I have this function which purpouse is to "separate" numeric characters and letters in 2 arrays and then printing them.

char separa(char arr[MAX_CAR-1]){
    char alpha[MAX_CAR];
    char num[MAX_CAR];
    int schiavo,schiavo2,schiavo3;
    int j=0;
    int k=0;
    int f=0;
    int lunghezza =strlen(arr);
    for (int i =0;i<lunghezza;i++) {
        schiavo = 0;
        schiavo2 = -2;
        schiavo3 = -2;
        schiavo = isalpha(arr[i]);
        schiavo2 = isspace(arr[i]);
        schiavo3 = isdigit(arr[i]);
        if (schiavo != 0 && schiavo2 == 0) {
            alpha[j] = arr[i];
            j++;
        } else if (schiavo3 != 0 && schiavo2 == 0) {
            num[k] = arr[i];
            k++;
        } else if (schiavo2 != 0) {
            f++;
        }
    }
}

So the thing is that i am supposed to print those arrays in the main but i have no idea how to return them, i've tried reading forums about using pointers but i can't understand how it works for arrays

I even tried to return 1 array per time instead of 2 but even that didn't work

cafce25
  • 15,907
  • 4
  • 25
  • 31
  • 1
    Is this really C++ as you tagged, because this code totally looks like plain C to me. – ypnos Nov 27 '22 at 23:01
  • 1
    You can't return a c-array. If this is c++ you may want to use `std::string` instead of your character arrays. – drescherjm Nov 27 '22 at 23:02
  • Wrap the arrays inside of a struct, fill the arrays. and return the struct. No pointers are needed. [See this answer](https://stackoverflow.com/questions/49341472/returning-a-2d-arrray-in-c/49342542#49342542) for a hint. – PaulMcKenzie Nov 27 '22 at 23:05
  • "Reading forums" is not the right way to learn C++. Have you tried [reading a good C++ textbook](https://stackoverflow.com/questions/388242/) instead? Because of what arrays are in C++ you can't meaningfully "return" them, in any way; however C++ textbook will explain several alternative ways of accomplishing whatever returning arrays is supposed to accomplish. – Sam Varshavchik Nov 27 '22 at 23:18
  • @SamVarshavchik thank you for the advice, i know reading forums it's not a good way to learn thing, but the problem is that since i'm in uni most of the time i study from slides and my professor's registrations so i really never had the idea to buy one, maybe you have one what can you advise me? – KyruaSama Nov 27 '22 at 23:22
  • @KyruaSama In other words, what if the requirements were to return two `int`'s? Or 3 `int`'s? Or an `int`, and a `double`? How do you return two (or more entities) at once? The approach to figure that out would be similar to how you would return two arrays, hint (again) -- use a `struct` or similar aggregate type. – PaulMcKenzie Nov 27 '22 at 23:25
  • @PaulMcKenzie Ty for all the help, i'm sorry to bother you even more but i really do not know where to smash my head anymore, i tried asking in my uni group chat if this kind of solution is fine but a lot of people actually reaplied me that he actually want us to return the number of elements in Alpha and Num, and then in the main print somehow in other 2 arrays, he called them SAlpha and SNum, but i really can't understand what's the point of returning the number of elements in the arrays and don't even know how to do it,i feel so abd asking it like this but i really can't understand – KyruaSama Nov 27 '22 at 23:35
  • *but a lot of people actually reaplied me that he actually want us to return the number of elements in Alpha and Num* -- You can only return one *entity* in a function. The keyword is *entity* -- again, that entails knowing what a `struct` is. Do you know what `structs` are? If not, I suggest going back to the C++ book you're using and check the chapter on this. There is no way to individually return two items in C++ -- that is a non-starter. – PaulMcKenzie Nov 27 '22 at 23:48
  • Did you open the link in my previous comment, the one you replied to? – Sam Varshavchik Nov 27 '22 at 23:50

1 Answers1

2

An approach you can use is to initialize the 2 arrays: alpha[MAX_CAR] and num[MAX_CAR] as a global variable outside of the function sepera. To use this approach, what you can do is have void separa(char arr[MAX_CAR-1], char alpha[MAX_CAR], char num[MAX_CAR]). This way, you can store these global variables as arguments to the function sepera, and it should work. An approach would work like this (I haven't tested this code):
Somewhere in main:

char alpha[MAX_CAR]
char num[MAX_CAR]

Then write your function looking something like this:

void separa(char arr[MAX_CAR-1],char alpha[MAX_CAR], char num[MAX_CAR]){
    int schiavo,schiavo2,schiavo3;
    int j=0;
    int k=0;
    int f=0;
    int lunghezza =strlen(arr);
    for (int i =0;i<lunghezza;i++) {
        schiavo = 0;
        schiavo2 = -2;
        schiavo3 = -2;
        schiavo = isalpha(arr[i]);
        schiavo2 = isspace(arr[i]);
        schiavo3 = isdigit(arr[i]);
        if (schiavo != 0 && schiavo2 == 0) {
            alpha[j] = arr[i];
            j++;
        } else if (schiavo3 != 0 && schiavo2 == 0) {
            num[k] = arr[i];
            k++;
        } else if (schiavo2 != 0) {
            f++;
        }
    }
}

Hope this helps.

xingharvey
  • 131
  • 6