-->

WAP to input strings and sort them in alphabetical order. Pass string as a parameter.

in turboc++
---------------------------------------------------------------------------------------
//WAP to input strings and sort them in alphabetical order. Pass string as a parameter.
//wap to get length of string by passing string as parameter.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void stringsort(char array[40][40],int size);
//prototype with array string; we can also use simply array[]
int main()
{
char arr[40][40];            // array declaration with maximum limit 40
int i,n;
printf("enter total number of strings\n");
scanf("%d",&n);//size of total strings
printf("enter strings\n");
for(i=0;i<=n-1;i++)
{
scanf("%s",arr[i]);//gets string//input of string
}
stringsort(arr,n);//calling of function with argument
getch();
return 0;
}
void stringsort(char array[40][40],int size)//body line
{
    int i,j;
    char tempo[20];
    for(i=0;i<size;i++)
    {
        for(j=i+1;j<size;j++)//comaprison of strings
        {
            if((strcmp(array[i],array[j])>0))//condition testing
            {
                strcpy(tempo,array[i]);
                strcpy(array[i],array[j]);//string copying to different location
                strcpy(array[j],tempo);
            }
        }
    }
printf("sorted string are\n");//output of sorted string
for(i=0;i<=size-1;i++)
{
printf("%s\n",array[i]);
}
getch();
}
------------------------------------------------------------------------------------------------
in codeblocks:
-----------------------------------------------------------------------------------------------------
//WAP to input strings and sort them in alphabetical order. Pass string as a parameter.
//wap to get length of string by passing string as parameter.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void stringsort(char array[40][40],int size);//prototype with array string; we can also use simply array[]
int main()
{
char arr[40][40];            // array declaration with maximum limit 40
int i,n;
printf("enter total number of strings\n");
scanf("%d",&n);//size of total strings
printf("enter strings\n");
for(i=0;i<=n-1;i++)
{
scanf("%s",arr[i]);//gets string//input of string
}
stringsort(arr,n);//calling of function with argument
return 0;
}
void stringsort(char array[40][40],int size)//body line
{
    int i,j;
    char tempo[20];
    for(i=0;i<size;i++)
    {
        for(j=i+1;j<size;j++)//comaprison of strings
        {
            if((strcmp(array[i],array[j])>0))//condition testing
            {
                strcpy(tempo,array[i]);
                strcpy(array[i],array[j]);//string copying to different location
                strcpy(array[j],tempo);
            }
        }
    }
printf("sorted string are\n");//output of sorted string
for(i=0;i<=size-1;i++)
{
printf("%s\n",array[i]);
}
getch();
}

No comments:

Post a Comment