Sort a Strin

QUESTION

Given a string S consisting of lowercase Latin letters, arrange all its letters in ascending order. \n\nInput:\n\nThe first line of the input contains T denoting number of testcases.Then follows description of each testcase.The first line of the testcase contains positive integer N denoting the length of string.The second line contains the string S.\n\n\nOutput:\n\nFor each testcase,output the sorted string.\n\n\nConstraints:\n\n1<=T<=100\n1<=N<=100.

“TESTCASE_1”: “1\n5\nedcab\n###—###SEPERATOR—###—\nabcde”, “TESTCASE_2”: “2\n6\nstring\n4\ndcba\n###—###SEPERATOR—###—\nginrst\nabcd”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <iostream>
using namespace std;
int main()
{int n,t;
 char ch[100];
 cin>>t;
 for(int i=0;i<t;i++)
 {cin>>n;
  for(int j=0;j<n;j++)
    cin>>ch[j];
  char c;
  for(int j=0;j<n-1;j++)
  {for(int k=0;k<n-j-1;k++)
   {if(ch[k]>ch[k+1])
    {c=ch[k];
     ch[k]=ch[k+1];
     ch[k+1]=c;
    }
   }
  }
  for(int j=0;j<n;j++)
   cout<<ch[j];
  cout<<'\n';
 }
 

	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