I'm preparing for an interview and while looking across candidate experiences i came across the following question -
Given an array of strings : { “Sdjksajd”, “Skjdasjj”, “Bjsdakja”, “Xhudhau”, “Eeeeggg”, “Chrome”}. Display them in alphabetical order without using library functions .
I'm not very good at coding so i tried doing this using my limited knowledge in the following way:-
#include<iostream>
#include<string>
using namespace std;
string selsort(string s);
string selsort(string s)
{
int i=0,j,k=0;
string min=&s[i];
for(i=0;i<6;i++)
{
for(j=i+1;j<6;j++)
{
if(s[j][k]<s[i][k])
{
string *temp;
min=s[j];
*temp=s[i];
s[i]=s[j];
s[j]=*temp;
}
else if(s[j][k]=s[i][k])
{
while(k<=s[j].length())
{
k++;
selsort(string s);
}
}
}
}
return s;
}
int main()
{
int i;
string s[6]={"Sdjksajd","Skjdasjj","Bjsdakja","Xhudhau","Eeeeggg","Chrome"};
s=selsort(s);
for(i=0;i<6;i++)
cout<<s[i];
return 0;
}
However, i am getting "Error: invalid types 'char[int]' for array subscript" and i think my program needs many more modifications to make it work. How exactly can i solve this question?