-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
30 lines (30 loc) · 1.19 KB
/
Copy pathsort.cpp
File metadata and controls
30 lines (30 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//sorting input string by ascending order of characters using vectors
//vector using a function
#include<iostream>
using namespace std;
string sortchar( string s) //declaring a function
{
vector <int> freq(26,0); //declaring a vector freq(26,0) as the numbering of charachters from 0 upto 26, or a to z
for(int i =0;i< s.length();i++) //loop upto the length of the string that we will input in the main function
{
int index = s[i] - 'a'; //we subtract a to get index of first chrachter as 0 and adding +1 as index of further charchtes i.e a-a =0, b-a =1, c-a =2...
freq[index]++; //incrementing to get to 26 i.e index of Z
}
int j = 0;
for(int i =0; i<26;i++)//loop for indexes of all charachters from 0 to 25
{
while(freq[i]--) //while loop for condition freq vector decrementing
{
s[j++] = i + 'a'; //for inserting the charachter as many times as necessary until the frequency of charachter becomes 0 with respect to input
}
}
return s;
}
int main()
{
string st;
cout<<"Enter string to sort in ascending order of charachters : ";
cin>>st;
cout<<"Ascending Order: "<<sortchar(st);//calling function
return 0;
}