Sort the N Names in an Alphabetical Order

QUESTION

Program sorts the names in an alphabetical order. The program accepts names & then sorts the names in an alphabetical order using string operation.

“TESTCASE_1”: “2\nab\nba\n###—###SEPERATOR—###—\nab\nba”, “TESTCASE_2”: “5\nraj\nchris\nben\nabhay\ngautam\n###—###SEPERATOR—###—\nabhay\nben\nchris\ngautam\nraj”, “TESTCASE_3”: “4\nabcd\ngcbd\nbacd\nkldf\n###—###SEPERATOR—###—\nabcd\nbacd\ngcbd\nkldf”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
#include <string.h>
int main()
{
  int n,i,j;
  scanf("%d",&n);
  char str[100][100],temp[100];
  for(i=0;i<n;i++)
  {for(j=0;j<n;j++)
    scanf("%s",str[j]);
  }
  
  /*for(j=0;j<n;j++)
    printf("%s\n",str[j]);
  */
 for(i=0;i<n;i++)
  {
    for(j=0;j<n-i-1;j++)
    {
      if(strcmp(str[j],str[j+1])>0)
      {strcpy(temp,str[j]);
       strcpy(str[j],str[j+1]);
       strcpy(str[j+1],temp);
      }
    }
  }
  for(i=0;i<n;i++)
    printf("%s\n",str[i]);

	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock