-->

Write a program to sort 'n' strings. (ascending order or descending order).

in turboc++
------------------------------------------------------------------------------------
//WAP  to input some strings and sort them.
#include <stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char string[100][100],tempo[100];//two dimensional declaration
    int i,j,n;
    printf("enter n as total strings\n");
    scanf("%d",&n);
    printf("enter strings\n");
    for(i=0;i<=n-1;i++)
    {
        printf("string for location =%d \n",i);
        scanf("%s",string[i]);//gets inputs
    }
    printf("names and addresses are=\n");
    for(i=0;i<=n-1;i++)
    {
        printf("name =%s \n",string[i]);//prints the data .
    }
    printf("sorted strings are\n");
    for(i=0;i<=n-1;i++)
    {
        for(j=i+1;j<=n-1;j++)
        {
            if(strcmp(string[i],string[j])>0)//comparison of strings
            {
                strcpy(tempo,string[i]);
                strcpy(string[i],string[j]);//copy of string i.e. swapping of string
                strcpy(string[j],tempo);
            }
        }
    }
    printf("-------------------------------");
     for(i=0;i<=n-1;i++)
    {
        printf("name =%s \n",string[i]);//prints the data .
    }
getch();
     return 0;
}
-------------------------------------------------------------------------------------
in codeblocks:
----------------------------------------------------------------
//WAP  to input some strings and sort them.
#include <stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char string[100][100],tempo[100];//two dimensional declaration
    int i,j,n;
    printf("enter n as total strings\n");
    scanf("%d",&n);
    printf("enter strings\n");
    for(i=0;i<=n-1;i++)
    {
        printf("string for location =%d \n",i);
        scanf("%s",string[i]);//gets inputs
    }
    printf("names and addresses are=\n");
    for(i=0;i<=n-1;i++)
    {
        printf("name =%s \n",string[i]);//prints the data .
    }
    printf("sorted strings are\n");
    for(i=0;i<=n-1;i++)
    {
        for(j=i+1;j<=n-1;j++)
        {
            if(strcmp(string[i],string[j])>0)//comparison of strings
            {
                strcpy(tempo,string[i]);
                strcpy(string[i],string[j]);//copy of string i.e. swapping of string
                strcpy(string[j],tempo);
            }
        }
    }
    printf("-------------------------------");
     for(i=0;i<=n-1;i++)
    {
        printf("name =%s \n",string[i]);//prints the data .
    }
getch();
     return 0;
}

No comments:

Post a Comment