In this Program pointer concept is been used with the use of variables of pointer type char and integer type which contain the values. Reorder () also used for sorting the the string as required. In the program string is to be entered after that loop statement works. During loop statement the pointer type variable acquires the memory using malloc() function.
The next move is to sort the string here loop statement again come in use and then printing the string. So on the next step string functions comes into the frame like Strcmp() or Strcpy() for conditionally checks the string. Here while using the string function if condition also comes into the place for checking if the string sorted or not in the last the sorted string is to be displayed as output.
Problem Statement:
Here we have to make a Program to sort the String with using Pointer and string Functions.
- Entering the String.
- Using required functions.
- Using loop Statement.
- And Display The Output on the Screen.
The C program is successfully compiled. The program output is also shown below.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *x[20];
int i,n=0;
void reorder(int n,char *x[]);
clrscr();
printf("Enter no. of String : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the Strings %d : ",i+1);
x[i]=(char *)malloc(20*sizeof(char));
scanf("%s",x[i]);
}
reorder(n,x);
printf("\nreorder list is : \n");
for(i=0;i<n;i++)
{
printf("%d %s\n",i+1,x[i]);
}
getch();
}
void reorder(int n,char *x[])
{
int i,j;
char t[20];
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
strcpy(t,x[j]);
strcpy(x[j],x[i]);
strcpy(x[i],t);
}
return;
}