Join Regular Classroom : Visit ClassroomTech

Searching & Sorting | Words & Numbers Sorting | Codewindow.in

Given required text comprising of words & numbers, sort them both in increasing order & shown them in a manner that a word is followed by a number. Words can be in upper case or lower case. we need to convert them into lowercase as well as sort then and then print them.

Input Format:
First line: Total number of test cases N Next N lines each line contains a text in which words are at odd position & no. are at even position & are delimited by space.

Output Format:
Words & numbers sorted in increasing order & printed in a manner Words & numbers sorted in increasing order & printed in a manner.

Constraints:
1. Text starts with a word
2. Count of words & no. are the same.
3. Duplicate elements should not allowed
4. Words must be show in lower case.
5. In text No special characters allowed .
Sample Input and Output

Example:
Input:
2
Sagar 35 sanjay 12 ganesH 53 ramesh 19
Ganesh 59 suresh 19 rakesh 26 laliT 96

Output:
ganesh 12 ramesh 19 sagar 35 sanjay 53
ganesh 19 lalit 26 rakesh 59 suresh 96

Solution: In C

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main()
{
   int t;
   scanf("%d",&t);
   while(t--){
        char s[100][100],t[100],e[100][100],o[100][100];
        int i=0,j,k=0,swap,p,n,c,d,m=0,q,w,a[1000];
        do
        {
            scanf("%s %d",s[i],&a[i]);
            i++;
        } while(getchar() != '\n');
        for(w=0;w<i;w++)
        {
            q=0;
            while(s[w][q] != '\0')
            {
                s[w][q] = tolower(s[w][q]);
                q++;
            }
        }
        for(w=1;w<i;w++)
        {
            for(j=1;j<i;j++)
            {
                if(strcmp(s[j-1],s[j])>0)
                {
                    strcpy(t,s[j-1]);
                    strcpy(s[j-1],s[j]);
                    strcpy(s[j],t);
                }
            }
        }
        for (c = 0 ; c < i-1; c++)
        {
            for (d = 0 ; d < i - c - 1 ; d++)
            {
                if (a[d] > a[d+1])
                {
                    swap=a[d];
                    a[d]=a[d+1];
                    a[d+1]=swap;
                }
            }
        }
        for(w=0;w<i;w++)
        {
            printf("%s %d ",s[w],a[w]);
        }
        printf("\n");
   }
}

Also Checkout

Recent Posts
Categories
Pages