Followers

Monday, March 12, 2012

COMPARING 2 STRINGS FOR EQUALITY

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


int len1, len2;

void equal(char a[50], char b[50])
{
  if(len1==len2)
  {
    for(int i=0 ; i<len1 ; i++)
    {
      if(a[i]==b[i])
      continue;
      else
      {
      cout<<"NOT EQUAL";
      getch();
      exit(0);
      }
    }
    cout<<"EQUAL";
  }
  else
    cout<<"NOT EQUAL";

}

void main()
{
  char strng1[25], strng2[25];
  cout<<"enter a string : ";
  gets(strng1);
  len1=strlen(strng1);
  cout<<"enter another string : ";

  gets(strng2);
  len2=strlen(strng2);
  equal(strng1, strng2);

  getch();
}
.................................................................
OUTPUT :(click to enlarge the image)

2 comments:

  1. asim string len check karne ki jarurat nahi thi
    kyuki agar ek string khattam ho gayi toh next value null hogi par us string ki dusri value kuch aur hogi

    ReplyDelete
    Replies
    1. that comparison makes the output faster in case of strings with different lengths

      Delete