Hi
I am trying to sort string arrays (a to z) .
They are up to 1000 array like these:
Fhfhfhd
Dtbktdhjldfv
Shmb
Svb
(just random strings )
how can i sort them?
Thanks
Well here is the code , it will first get the number of strings then a char, after that search if any string start with that char , after that it MUST sort ans from array 0 to z then cout ans (0-z)
#include <iostream>
#include<string>
#include <algorithm>
using namespace std;
string str[1000];//keep the input
string ans[1000];//keep str start with requested char
int main() {
int a;
cin >> a;//get number of str
for(int i = 0 ; i<a; i++) //get str
cin >> str[i];
char n;
cin >> n;//get requested char
int z=0;//count arrays which stt with char.
for (int i = 0 ; i<a; i++){
if(str[i].at(0)==n){
ans[z]=str[i];//put the str start with char in the first empty place of ans array
z++;
}
}
sort( ans[0],ans[z]);//sort array -----------have problem here
for (int i =0;i<=z;i++) cout << ans[i];//print the sorted str start with requested char
return 0;
}
1. Don't use arrays like that, it's wasteful. Use vectors instead. And don't make them global.
2. Use proper variable names. a, ans, str, z don't qualify in regards of their intended use.
3. <= in the last line should be <.
4. sort expects iterators (so, pointers in this case).