Checking for equality of two strings without recursion

Problem statement
Write a c program to check weather two given character strings are same or not,
Your program should ask the user to give input
#include<stdio.h>
int compair(char str1[],char str2[]);
int main()
{
  char str1[20],str2[30];
  int y;
  printf("enter string 1 ");
  scanf("%s",str1);
  printf("enter string 2 ");
  scanf("%s",str2);
  y=compair(str1,str2);
  if(y==1)
    {
      printf("strings arte same \n");
    }
  else
    {
      printf("string are different \n");
    }
  return 0;
}
int compair(char str1[],char str2[])
{
  int i=0;
  while(str1[i]!='\0')
    {
      if(str1[i]==str2[i])
    {
      i++;
    }
      else
    {
      return 0;
    }
    }
  if(str2[i]=='\0')
    {
      return 1;
    }
  else
    {
      return 0;
    }
}



1 comment:

  1. good example vary useful to me thanx admin

    ReplyDelete