string sorting in c

 Problem Statement:

The program must accept N String values containing only alphabets as the input. The program must sort sort the given string values in descending order based on the number of upper case alphabets. If two or more string values have the same number of upper case alphabets, then program must sort those strings value in the order of their occurrence.

Solution

 #include<stdio.h>

#include <stdlib.h>

int main()

{

int n,i,j,t,count,t2;

scanf("%d",&n);

int a[n],b[n];

char s[n][101],t1[101];

for(i=0;i<n;i++)

{

    scanf("%s",&s[i]);

    count=0;

    for(j=0;j<strlen(s[i]);j++)

    {

        if(s[i][j]>=65&&s[i][j]<=90)

        count++;

    }

    a[i]=count;

    b[i]=i;

}

for(i=0;i<n;i++)

{

    for(j=i+1;j<n;j++)

    {

        if(a[i]<a[j])

        {

            t=a[i];

            a[i]=a[j];

            a[j]=t;

            strcpy(t1,s[i]);

            strcpy(s[i],s[j]);

            strcpy(s[j],t1);

            t2=b[i];

            b[i]=b[j];

            b[j]=t2;

        }

        else if(a[i]==a[j])

        {

            if(b[i]>b[j])

            {

                t2=b[i];

                b[i]=b[j];

                b[j]=t2;

                strcpy(t1,s[i]);

                strcpy(s[i],s[j]);

                strcpy(s[j],t1);

            }

        }

    }

}

for(i=0;i<n;i++)

{

    printf("%s\n",s[i]);

}

}

Input

3

APple

BaSkeT

sKaTeBoArD

Ouput

sKaTeBoArD

BaSkeT

APple

Rajesh

This Blog will help you to find out the c programs solution easily and also you can able to understand the program.Please follow our blog for more useful updates.

Post a Comment (0)
Previous Post Next Post