Challenge String Sort

The first case testcase fails…any error in my code??

https://ide.codingblocks.com/s/146475

Hello @prashantverma.vn,

You are only checking for the first character of both the strings.
Thus, it if failing for the cases:
4
bat
apple
batman
bann
Expected Output:
apple
bann
batman
bat
Your Output:
apple
batman
bat
bann

Hope, this would help.
Give a like if you are satisfied.

I’ve understood my mistake but still stuck in how to correct my code.
Can you help me out in correcting my code??

Sure @prashantverma.vn,

The following code will

  1. compare both the strings character by character.
  2. In case of a prefix:
    The first two if and else-if conditions are used to find which string among a and b is a prefix.
  3. Else, sort lexicographically.

bool mycompare(string a,string b){
int i=0;
while(a[i]==b[i]) i++;
if(a[i]==’\0’) return false;
else if(b[i]==’\0’) return true;
return a < b;
}

Hope, this would help.
Give a like if you are satisfied.

I think I did something wrong can you please check, the output is incorrect
https://ide.codingblocks.com/s/146513

Hello @prashantverma.vn,

You have done a logical error in your compare function.

The while loop is used to compare the two strings.
You are not required to include the if-else statements inside it.

Suggestion:
Dry run compare function for any two strings using pen and paper to understand the logic.

Hope, this would help.
Give a like if you are satisfied.

1 Like

Thanxx for your help and suggestion:)

Anytime @prashantverma.vn,

Please, mark this doubt as resolved.