What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

C & C++ How to make Sorting and Searching Program.

J

Jeanh

, Last edited by a moderator:
How to make Sorting and Searching Program.



Sorting and Searching is an important task in You do not have permission to view the full content of this post. Log in or register now.. Here we will see such a You do not have permission to view the full content of this post. Log in or register now.. This program is used to sorting ands earching a string array. Running this program we will provide names and given names are sorted and searched by the program.You do not have permission to view the full content of this post. Log in or register now. is given bellow?

Code:
#include<stdio.h> 

#include<conio.h>
#include<string.h>

struct name // Custom Data Type
{
chara[10];

}b [10]; // Custom data type’s variable

void main()
{

int N,i,j;
intc=0;
char name[10];
clrscr();
printf("How many names :");
scanf("%d",&N); fflush(stdin);
for( i=0;i<N;i++)
{
printf("Enter %d Name :",i+1);
gets(b[i].a);
}

printf("\n\nEntered name list :");
for(i=0;i<N;i++)
{
printf("\n%s",b[i].a);
}

for(i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if((strcmp(b[i].a,b[j].a))>0)
char temp[10];
strcpy( temp,b[i].a);
strcpy(b[i].a,b[j].a);
strcpy( b[j].a,temp);
}
}
}
printf("\n\nSorted name list :");
for(i=0;i<N;i++)
{
printf("\n%s",b[i].a);
}



printf("\n\nEnter name to search :");
gets(name);
for(i=0;i<N;i++)
{
if((strcmp(b[i].a,name))==0)
c++;
}
if(c==0)
{
printf("%s not found .",name);
}
else
{
printf("%s remains %d times .",name,c);
}
getch();
}
Output:

http://2.bp.phc.onl/#forbidden#/-Bo8sYAJTkhw/Th4HbPrupdI/AAAAAAAAABg/-MLDLmSyYMs/s1600/sorting_search.JPG


Remarks

After giving name(You do not have permission to view the full content of this post. Log in or register now.) we can see the list of name and then sorted list. When we will give a name and search, we will see that name if it is in array otherwise name not found on the screen. Here strcpy () You do not have permission to view the full content of this post. Log in or register now.is used to copy one variable’s value another variable and strcmp() You do not have permission to view the full content of this post. Log in or register now. is used to compare two string. fflushYou do not have permission to view the full content of this post. Log in or register now.) is used to flush buffer.

 

Online statistics

Members online
1,500
Guests online
966
Total visitors
2,466

Forum statistics

Threads
813,119
Messages
16,086,177
Members
1,436,703
Top