My TestCases are failing in this code, could not understand why? Can you please debug and tell me where am I going wrong.
#include
#include
#include
#include<string.h>
using namespace std;
int main()
{
int n;
cin >> n;
char str[n][20];
for(int i=0;i<n;i++)
cin>> str[i];
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j]))
{
char s[20];
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
for(int i=0;i<n-1;i++)
{
int count=0;
for(int j=0;j<strlen(str[i]);j++)
{
if(str[i][j]==str[i+1][j])
{
count++;
}
}
if(count==strlen(str[i]))
{
char s[20];
strcpy(s,str[i+1]);
strcpy(str[i+1],str[i]);
strcpy(str[i],s);
}
}
for(int i=0;i<n;i++)
{
cout<<endl<<str[i];
}
return 0;
}