Monday, June 18, 2012

To compare two strings


/* Program to compare two strings */

#include<stdio.h>

#include<conio.h>

void main()

{

  char str1[20],str2[20];

  int i=0;

  clrscr();

  printf("Enter first string:");

  gets(str1);

  printf("Enter second string:");

  gets(str2);

  while(str1[i]==str2[i])

  {

    if(str1[i]=='\0' || str2[i]=='\0')

      break;

    i++;

  }

  if(str1[i]=='\0' && str2[i]=='\0')

    printf("\nStrings are equal");

  else

    printf("\nStrings are not equal");

  getch();

}

 

OUTPUT 1

Enter first string:iteration

Enter second string:recursion

 

Strings are not equal

 

OUTPUT 2

Enter first string:goodmorning

Enter second string:goodmorning

 

Strings are equal

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.