Answer to Question #346480 in C++ for neher

Question #346480

An array my_Array[] consisting of ‘a’ s, ‘b’ s and ‘c’s. The task is to write a function that arranges the array such that all ‘a’s are placed first, then all ‘b’s and then all ‘c’s in last. 


1
Expert's answer
2022-05-30T15:05:34-0400
#include <iostream>


using namespace std;


int main() {
    int n, a = 0, b = 0, c = 0;
    cout << "Enter number elements" << endl;
    cin >> n;
    char array[n];
    for (int i = 0; i < n; i++) {
        cin >> array[i];
        if (array[i] == 'a') {
            a++;
        }
        else {
            if (array[i] == 'b') {
                b++;
            }
            else {
                c++;
            }
        }
    }


    char result[n];
    for (int i = 0; i < a; i++) {
        result[i] = 'a';
    }
    for (int i = a; i < a + b; i++) {
        result[i] = 'b';
    }
    for (int i = a + b; i < n; i++) {
        result[i] = 'c';
    }


    for (int i = 0; i < n; i++) {
        cout << result[i] << " ";
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS