Saturday, 14 February 2015

Program for string comparison [strcmp()] without using string.h header file.

Program for string comparison [strcmp()] without using string.h header file.

#include<iostream.h>
#include<conio.h>
int strcmp(int a[],int b[]);
void main()
{
 int str1[40],str2[40];char ch;
 int res;
 clrscr();
 int i=0;
 cout<<"Enter two string: ";
 while((ch=getchar())!='\n')
    {
       str1[i]=ch;
        i++;
    }
 str1[i]='\0';
 int j=0;
 while((ch=getchar())!='\n')
   {
     str2[j]=ch;
     j++;
    }
 str2[j]='\0';
 res=strcmp(str1,str2);
 cout<<"Compared String result:"<<res;
 getch();
 }
int strcmp(int a[],int b[])
 {
   int i=0,count=0,count1=0;
   while(a[i]!='\0')
 {
   count=count+1;
    i++;
 }
 i=0;
 while(b[i]!='\0')
  {
   count1=count1+1;
   i++;
  }
if(count==count1)
return 0;
if(count>count1)
return 1;
else
return -1;
}

No comments:

Post a Comment