Pages

Thursday, July 14, 2011

Sorting and Searching Program with C and C++

Sorting and Searching is an important task in computer programming. So, this article will show how to do a sorting or searching program with C and C++ language. The following program will first store some strings into an array and then this array will be sorted with sorting algorithm. This program also stores user searching input and then searches the string in array.  If the search keyword is matched, the program will show how many times the search keyword is matched. Otherwise, search keyword not found message will be shown. 

Sorting and Searching program with C and C++  

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

  struct name            // Custom data type and variable
  {
      char a[10];   
       
   }b [10];        
       
void main()

  int N,i,j;
  int c=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();
 }

Program Output



11 comments:

Jannati said...

Nice Posting....

Sayeed said...

Thanx

Rana said...

Nice work for programming....

SEO Bangalore said...

Genious.. Programming is awesome...

SEO Company in Bangalore

ShadowsNose said...

Thank you for this, I was looking for a place to start learning and this is it! I think I will be coming here a lot for information.

Tomasz Ch Poland said...

for (j=(i+1);j0)

india walkins said...

Very nice one.


Thank You.

Unknown said...

your services very super

Seo company bangalore

Unknown said...

your web services very super

Seo companies bangalore

heemanshu bhalla said...

Nice
www.geeksprogrammings.blogspot.in

Student IDentity said...

I have ended in this blog when i have searched the Best Signs or C++ Tutorial for my friend. thanks.