1.1.5 Two-Dimensional character Array operations (Searching Elements, Copying, Merging, Finding length of given string)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char fruits[5][10],ans,tmp[10],ser[10],rep[10];
 int i,j,ch,len,flag=0,loc;
 clrscr();
 for(i=0;i<5;i++)
 {
  printf(“Enter fruits:”);
  scanf(“%s”,fruits[i]);
 }
 do
 {
  printf(“\n1.Sorting”);
  printf(“\n2.Display”);
  printf(“\n3.Find Length”);
  printf(“\n4.Search”);
  printf(“\n5.Copy the name at specified location”);
  printf(“\nEnter Ur Choice:”);
  scanf(“%d”,&ch);
  switch(ch)
  {
   case 1:
    for(i=0;i<5;i++)
    {
     for(j=i+1;j<5;j++)
     {
       if(strcmp(fruits[i],fruits[j])>0)
       {
          strcpy(tmp,fruits[i]);
          strcpy(fruits[i],fruits[j]);
          strcpy(fruits[j],tmp);
       }
     }
   }
  break;
 case 2:
 for(i=0;i<5;i++)
 {
    printf(“\n%s”,fruits[i]);
  }
  break;
 case 3:
  for(i=0;i<5;i++)
  {
    len=strlen(fruits[i]); 
    printf(“\nLength of %s is %d”,fruits[i],len);
  }
  break;
 case 4:
   printf(“\nEnter fruit name that you want to search:”);
   scanf(“%s”,ser);
   flag=0;
   for(i=0;i<5;i++)
  {
    if(strcmpi(fruits[i],ser)==0)
   {
     flag=1;
     break;
   }
 }
 if(flag==1)  
   printf(“\n%s is located at %d”,ser,i+1);
 else
   printf(“\n%s is not available”,ser);
 break;
case 5:
  printf(“\nEnter fruit name that you want to copy:”);
  scanf(“%s”,rep);
  printf(“\nEnter location:”);
  scanf(“%d”,&loc);
  if(loc>5)
  {
    printf(“\nLocation is Invalid”);
   }
   else
  {
    strcpy(fruits[loc-1],rep);
  }
  break;
 default:
   printf(“You entered wrong choice”);
 }
 printf(“\nDo you want to Continue,Press Y or y:”);
 fflush(stdin);
 scanf(“%c”,&ans);
 }while(ans==’y’ || ans==’Y’);
 getch();
}

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *