Wednesday, August 21, 2013

Write a program to perform various string operations such as Copy,Length,Reversing, Palindrome, Concatenation and to find occurrence substring etc with and without using library functions


/*-------------------------------------------------------------------------------------------------

Program   :Write a program to perform various string operations such as Copy,
                    Length,Reversing, Palindrome, Concatenation and to find occurrence
                    substring etc with and without using library functions.

Created By: JABIR DAUD PATHAN
Branch       : COMPUTER ENGINEERING
---------------------------------------------------------------------------------------------------*/

/*--------------<<  Header File  >>--------------------*/

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

/*-------<< Global Declaration of Functions  >>---------*/

void menu(void);
void accept1(void);
void accept2(void);
void strlibcopy(void);
void strliblen(void);
void strlibrev(void);
void strlibpalin(void);
void strlibcat(void);
void strlibsub(void);
void copy(void);
void length(void);
void reverse(void);
void palindrome(void);
void concatenation(void);
void substring(void);

char str1[25],str2[25],str3[25],i=0,j=0,flag=0;
                                                            //Globle declaration of variable

/*---------<< function defination for menu >>-----------*/

void menu()
 {
  printf("\n\n\n\t\t----------<<  SUB-MENU  >>--------------");
  printf("\n\n\t\t1.Copy of one string into another string");
  printf("\n\t\t2.Find out length of the string");
  printf("\n\t\t3.Reverse the string");
  printf("\n\t\t4.Find out the string is palindrome or not");
  printf("\n\t\t5.Concatenate of two strings");
  printf("\n\t\t6.Find enter string is substring or not");
  printf("\n\t\t7.Exit");
  printf("\n\n\t\t-----------------------------------");
  printf("\n\n\t\tEnter your choice==>");
 }

/*------<< function defination for accept 1st string >>------*/

void accept1()
 {
  printf("\nEnter 1st string==>\n");
  scanf("%s",&str1);
 }

/*------<< function defination for accept 2nd string >>-----*/

void accept2()
 {
  printf("\nEnter 2nd string==>\n");
  scanf("%s",&str2);
 }

/*-------<< function defination for copy string >>--------*/

void strlibcopy()
 {
  printf("\nBefore copy str2 into str1==>\n\nstr1==>%s\nstr2==>%s\n",str1,str2);
  strcpy(str1,str2);
  printf("\nAfter copy str2 into str1==>\n\nstr1==>%s\nstr2==>%s\n",str1,str2);
 }

/*------<< function defination for string length >>-------*/

void strliblen()
 {
  printf("\nLength of the string %s==>%d",str1,strlen(str1));
 }

/*------<< function defination for reverse string >>------*/

void strlibrev()
 {
  strrev(str1);
  printf("\nThe reverse string is==>%s",str1);
 }

/*----<< function defination for palindrome string >>----*/

void strlibpalin()
 {
  strcpy(str2,str1);
  strrev(str2);
  if(strcmp(str1,str2)==0)
     {
             printf("\n%s is palindrome string",str1);
     }
  else
     {
             printf("\n%s is not palindrome string",str1);
     }
 }

/*------<< function defination for concatenate string >>----*/

void strlibcat()
 {
  printf("\nBefore concatenate str2 with    str1==>\n\nstr1==>%s\nstr2==>%s\n",str1,str2);
  strcat(str1,str2);
  printf("\nAfter concatenate str2 with str1==>\n\nstr1==>%s\nstr2==>%s\n",str1,str2);
 }

/*---------<< function defination for substring >>---------*/

void strlibsub()
 {
  while(str1[i]!=NULL&&str2[i]!=NULL) //test condn till the end of str1 and str2
   {
    if(strstr(str1,str2))
     {
      flag=1;
     }
    else
     {
      flag=2;
     }
     i++;
   }
   if(flag==1)
    {
     printf("\n%s is substring of %s",str2,str1);
    }
   else
    {
     printf("%s is not substring of %s",str2,str1);
    }
 }

/*-------<< function defination for copy string >>----------*/

void copy()
 {
  printf("\nBefore copy str2 into str1==>\n\nstr1==>%s\nstr2==>%s\n",str1,str2);
  for(i=0;i<=24;i++)
   {
    str1[i]=str2[i];
   }
  printf("\nAfter copy str2 into str1==>\n\nstr1==>%s\nstr2==>%s\n",str1,str2);
 }

/*--------<< function defination for string length >>-------*/

void length()
 {
  int i=0;
  while(str1[i]!=NULL) //test condition till the end of string
   {
    i++;
   }
  printf("\nLength of the string %s==>%d",str1,i);
 }

/*------<< function defination for reverse string >>-------*/

void reverse()
 {
  int i=0,j=0;
  while(str1[i]!=NULL)
   {
    i++;
   }
  printf("\nThe string in reverse order is==>");
  for(j=i;j>=0;j--)
   {
    printf("%c",str1[j]);  //display string in reverse order
   }
 }

/*-----<< function defination for palindrome string >>-----*/

void palindrome()
 {
  while(str1[i]!=NULL) //test condition till the end of string
   {
    i++;
   }
  i--;
  while(j!=i)  
   {
    if(str1[i]==str1[j])
     {
      flag=1;
     }
    else
     {
      flag=2;
      break;
     }
     j++;
     i--;
    }
    if(flag==1)
     {
      printf("\n\n%s is palindrome string",str1);
     }
    else
     {
      printf("\n\n%s is not palindrome string",str1);
     }
  }

/*-----<< function defination for concatenate string >>-----*/

void concatenation()
 {
  int i=0,j=0;
  while(str1[i]!='\0') //test condition till the end of string str1
   {
    str3[i]=str1[i];
    i++;
   }
  while(str2[j]!='\0')  //test condition till the end of string str2
   {
    str3[i]=str2[j];
    i++;
    j++;
   }
  printf("\nThe concatenated string is:%s",str3);
 }

/*--------<< function defination for substring >>-------*/

void substring()
 {
   int i=0,j=0;
  while(str1[i]!=NULL&&str2[j]!=NULL) //test condition till the end of str1 and str2 string
   {
    if(str1[i]==str2[j])
     {
      flag=1;
      j++;
      i++;
     }
    else
     {
      i++;
      flag=2;
     }
   }
   if(flag==1)
    {
     printf("\n%s is substring of %s",str2,str1);
    }
   else
    {
     printf("\n%s is not substring of %s",str2,str1);
    }
 }

/*----------<<  Main Function Start  >>------------*/

void main()
 {
  int ch;  //Variable declaration
  char ans;
  do
   {
    clrscr();  //Clear the text mode of window
    printf("\n\t\t-------<<  MENU  >>---------");
    printf("\n\n\t\t1.With using library fuctions");
    printf("\n\t\t2.Without using library fuctions");
    printf("\n\t\t3.Exit");
    printf("\n\n\t\t-----------------------------");
    printf("\n\n\t\tEnter your choice==>");
    scanf("%d",&ch); //accept choice from user
    switch(ch)
     {
      case 1 :
                    do
                     {
                      clrscr();
                      printf("\n\n\t\t\t<<With using library fuctions>>");
                      menu();  //function call
                      scanf("%d",&ch);
                      switch(ch)
                             {
                                    case 1 :
                                                     accept1();
                                                     accept2();
                                                     strlibcopy(); //function call
                                                     break;
           
                                    case 2 :
                                                     accept1();
                                                     strliblen();
                                                     break;

                                     case 3 :
                                                     accept1();
                                                     strlibrev();
                                                     break;
           
                                     case 4 :
                                                     accept1();
                                                     strlibpalin();
                                                     break;
           
                                     case 5 :
                                                     accept1();
                                                     accept2();
                                                     strlibcat();
                                                     break;
           
                                     case 6 :
                                                     accept1();
                                                     accept2();
                                                     strlibsub();
                                                     break;
           
                                     case 7 :
                                                     exit(0);
                                                     break;
           
                             default :
                                                     printf("\nInvalid choice");
                                                     break;
                            }
                            printf("\n\n\t\t Do you Want to Continue(Y/N)?==>");
                            ans=getch();
                           }while(ans=='y'||ans=='Y');
                              break;
           
             case 2 :
                           do
                            {
                             clrscr();
                             printf("\n\n\t\t\t<<Without using library fuctions>>");
                             menu();    //function call
                             scanf("%d",&ch);
                             switch(ch)
                                    {
                                     case 1 :
                                                     accept1();
                                                     accept2();
                                                     copy();
                                                     break;
           
                                     case 2 :
                                                     accept1();
                                                     length();
                                                     break;
           
                                     case 3 :
                                                     accept1();
                                                     reverse();
                                                     break;
             
                                     case 4 :
                                                     accept1();
                                                     palindrome();
                                                     break;
             
                                     case 5 :
                                                     accept1();
                                                     accept2();
                                                     concatenation();
                                                     break;

                                     case 6 :
                                                     accept1();
                                                     accept2();
                                                     substring();
                                                     break;
           
                                     case 7 :
                                                     exit(0);
                                                     break;
           
                                    default :
                                                     printf("\nInvalid choice");
                                                     break;
                                    }
                                    printf("\n\n\t\tDo you Want to Continue(Y/N)?==>");
                                    ans=getch();
                            }while(ans=='y'||ans=='Y');
                               break;
           
              case 3 :
                             exit(0);
                             break;
           
             default :
                             printf("\nInvalid choice");
                             break;
           
            }
            printf("\n\n\nDo you Want to perform operation with/without using library functions(Y/N)?==>");
            ans=getch();
    }while(ans=='y'||ans=='Y');
     getch(); 
  }

/*------------<<  End Of Main Function  >>------------*/








/*-----------------<< OUTPUT SCREEN  >>-------------------*/

                -------<<  MENU  >>---------
                1.With using library fuctions
                2.Without using library fuctions
                3.Exit
                -----------------------------

                Enter your choice==>1


                     <<With using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>1

Enter 1st string==>
jabir

Enter 2nd string==>
pathan

Before copy str2 into str1==>

str1==>jabir
str2==>pathan

After copy str2 into str1==>

str1==>pathan
str2==>pathan

                 Do you Want to Continue(Y/N)?==>Y


                     <<With using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>2

Enter 1st string==>
jabir

Length of the string jabir==>5

                 Do you Want to Continue(Y/N)?==>Y


                     <<With using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>3

Enter 1st string==>
jabir

The reverse string is==>ribaj

                 Do you Want to Continue(Y/N)?==>Y



                     <<With using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>4

Enter 1st string==>
madam

madam is palindrome string

                 Do you Want to Continue(Y/N)?==>Y

                     <<With using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>5

Enter 1st string==>
jabir

Enter 2nd string==>
pathan

Before concatenate str2 with str1==>

str1==>jabir
str2==>pathan

After concatenate str2 with str1==>

str1==>jabirpathan
str2==>pathan

                 Do you Want to Continue(Y/N)?==>Y


                     <<With using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>6

Enter 1st string==>
maharashtra

Enter 2nd string==>
ash

ash is substring of maharashtra

                 Do you Want to Continue(Y/N)?==>N

Do you Want to perform operation with/without using library functions(Y/N)?==>Y




                -------<<  MENU  >>---------
                1.With using library fuctions
                2.Without using library fuctions
                3.Exit
                -----------------------------

                Enter your choice==>2


                    <<Without using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>1

Enter 1st string==>
jaihind

Enter 2nd string==>
engineering

Before copy str2 into str1==>

str1==>jaihind
str2==>engineering

After copy str2 into str1==>

str1==>engineering
str2==>engineering

                Do you Want to Continue(Y/N)?==>Y



                    <<Without using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>2

Enter 1st string==>
engineering

Length of the string engineering==>11

                Do you Want to Continue(Y/N)?==>Y


                   <<Without using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>3

Enter 1st string==>
jaihind

The string in reverse order is==> dnihiaj

                Do you Want to Continue(Y/N)?==>Y


                    <<Without using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>4

Enter 1st string==>
radar


radar is palindrome string

                Do you Want to Continue(Y/N)?==>Y

                    <<Without using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>5

Enter 1st string==>
jaihind

Enter 2nd string==>
engineering

The concatenated string is:jaihindengineering

                Do you Want to Continue(Y/N)?==>Y

                    <<Without using library fuctions>>


                ----------<<  SUB-MENU  >>--------------
                1.Copy of one string into another string
                2.Find out length of the string
                3.Reverse the string
                4.Find out the string is palindrome or not
                5.Concatenate of two strings
                6.Find enter string is substring or not
                7.Exit
                -----------------------------------------

                Enter your choice==>6

Enter 1st string==>
jaihind

Enter 2nd string==>
jai

jai is substring of jaihind

                Do you Want to Continue(Y/N)?==>N

Do you Want to perform operation with/without using library functions(Y/N)?==>Y

               -------<<  MENU  >>---------
               1.With using library fuctions
               2.Without using library fuctions
               3.Exit
               -----------------------------

               Enter your choice==>3





















No comments:

Post a Comment