Question # 1
Initialize a string of size 40. Write a program that prints all unique alphabets from string. After printing them sort them in ascending order.
For example: Hi world I am here to help you.
Unique characters are: H, I, W, O, R, L, D, A, M, E, T, P, Y, U.
Sorted: A, D, E, H, I , L,M, O, P, R, T, U, W, Y
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
void sortStrings(char arr[][MAX], int n)
{
char temp[MAX];
// Sorting strings using bubble sort
for (int j=0; j<n-1; j++)
{
for (int i=j+1; i<n; i++)
{
if (strcmp(arr[j], arr[i]) > 0)
{
strcpy(temp, arr[j]);
strcpy(arr[j], arr[i]);
strcpy(arr[i], temp);
}
}
}
cout<<arr<<endl;
}
int main(){
char arr[40] = "GeeksforGeeks";
int n = sizeof(arr)/sizeof(arr[0]);
for (int i=0; i<n; i++)
cout<<arr[i];
}
Comments
Leave a comment