Wednesday, August 17, 2011

QUES 9 STRING COMPARE AND COPY AND CONCATENATE


C LANGUAGE BOOK














WAP TO PERFORM STRING COMPARISON,COPY ONE STRING TO ANOTHER
STRING AND CONCATENATE ONE STRING AT THE END OF OTHER.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
void xstrcmp(char *s1,char *s2);
char str1[20];
char str2[20];
char target[20];


int n;

clrscr(); //CLEARS THE OUTPUT SCREEN
printf("enter the first string");
gets(str1); //FIRST STRING ENTERED BY THE USER.
printf("enter the second string");
gets(str2); //SECOND STRING ENTERED BY THE USER.

xstrcmp(str1,str2);

xstrcpy(target,str1);
printf("\ntarget string=%s",target); //PRINT THE TARGET STRING

printf("enter the number of characters u want to concatinate");


scanf("%d",&n);
xstrncat(str1,str2,n);

 printf("concatination string is:%s",str1);     //printing the concatinated string
 printf("\n \n \n \n press any key to exit");


getch();
}


 //function for comparing two string
 void xstrcmp(char *s1,char *s2) //FUNC. TAKING TWO CHAR POINTERS
    {
      int check;
     while((*s2!='\0')||(*s1!='\0'))
       {
         if(*s1==*s2) //CHECK FOR THE EQUALITY OF S1 AND S2
          {
         check=0;
         s1++; //INCREMENT S1
         s2++; //INCREMENT S2
           }
         else
         {
          check=1; //INITIALISE CHECK TO ONE
          break;
          }
         }
          if(check==0) //CHECKS FOR THE VALUE OF CHECK
          printf("Two strings are same");
          else
         {
           printf("Two strings are not same");
          }

         }

 //function for copy the string to another string.
void xstrcpy(char *t,char *s) //FUNC. TAKING TWO CHAR POINTERS
  {
    while(*s!='\0') //CHECKING S TO NULL ZERO
    {
      *t=*s;
      s++;  //INCREMENT THE S
      t++; //INCREMENT THE T
      }
       *t='\0';
    }


//function for concatinating the string
void   xstrncat(char*s1,char*s2,int n)   //declaring the function
 {
    int c=0;
    s1=s1+strlen(s1);
    while(c!=n)
   {
     *s1=*s2;
     s1++;
     s2++;
     c++;
   }
    *s1='\0';                         //2nd string terminated by null
 }











C LANGUAGE BOOK

No comments:

Post a Comment