Wednesday, 9 November 2011

Write a C program in C to carry out following operations on strings using library functions... 1) To concatenate a string s2 to string s1 2) To find the length of a given string 3) To compare two strings s1 nad s2 4) To copy a string s2 to another string s1


 # include<stdio.h>
 void main()
 {
  char a[20],b[20],c[20],d[20];
  int z1=0,z2=0;
  clrscr();

  printf("\nEnter First String : ");
  scanf("%s",&a);
  printf("\nEnter Second String: ");
  scanf("%s",&b);

  printf("\n*** Length *** \n");
  z1 = strlen(a);
  printf("\nLength of String1 = %d\n",z1);
  z2 = strlen(b);
  printf("\nLength of String2 = %d\n",z2);

  printf("\n*** Compare ***\n");
  if (z1>z2)
  {
    printf("\nString1 is greater than String2\n");
  }
  else if(z1<z2)
  {
    printf("\nString2 is greater than String1\n");
  }
  else if (z1==z2)
  {
   printf("\nBoth strings are equal....\n");
  }

  printf("\n*** Copy ***\n");
  printf("\nBefore Copy\n");
  printf("\nString1 = %s\n",a);
  printf("\nString2 = %s\n",b);
  printf("\nAfter Copy\n");
  z1 = strcpy(a,b);
  printf("\nString1 = %s\n",z1);
  printf("\nString2 = %s\n",b);

  printf("\n*** Concatenate ***\n");
  z1 = strcat(a,b);
  printf("\nThe concatinated value is %s\n",z1);

  getch();
 }

2 comments: