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.
#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] << " ";
}
}
Comments
Leave a comment