program for comparing 2 strings without strcmp()

Answers were Sorted based on User's Feedback



program for comparing 2 strings without strcmp()..

Answer / ankitecian

int StrCmp(const char *_input1, const char *_input2)
{
int _cntr1 = 0;
int _flg = 0;
while(*(_input1 + _cntr1) != NULL || *(_input2 + _cntr1) !
= NULL)
{
if(*(_input1 + _cntr1) != *(_input2 + _cntr1))
{
_flg = -1;
}
_cntr1++;
}
return _flg;
}

Is This Answer Correct ?    3 Yes 1 No

program for comparing 2 strings without strcmp()..

Answer / ruchi

#include<stdio.h>
#include<conio.h>
int main()
{
char a[20],b[20];
int l1,l2,i=0,j=0;
printf("\nEntert the first string ");
while((a[i++]=getchar())!='\n');
l1=i-1;
printf("\nEnter the second string ");
while((b[j++]=getchar())!='\n');
l2=j-1;
if(l1>l2)
{
printf("\nThe first string is greater than the
second ");
}
else if(l2>l1)
{
printf("\n Second string is greater than the first ");
}
else
{
printf("\nBoth the strings are equal ");
}
getch();
}

Is This Answer Correct ?    2 Yes 1 No

program for comparing 2 strings without strcmp()..

Answer / bin

Here is a subroutine that does strcpy, returns 0 if both are equal, returns a positive number, if arg1 is higher else returns a negative number


int mystrcmp(char *s1, char *s2)
{
while (*s1 != 0 && *s2 != 0 && *s1 == *s2) {
++s1; ++s2;
}
return (*s1) - (*s2);
}

Is This Answer Correct ?    1 Yes 1 No

program for comparing 2 strings without strcmp()..

Answer / karthikeyan n p

#include<stdio.h>
#include<conio.h>
void main()
{
char st1[10],st2[10];
int i,sl1=0,sl2;
clrscr();
printf("\nEnter String1:");
scanf("%s",st1);
printf("\nEnter String2:");
scanf("%s",st2);
for(i=0;st1[i]!='\0';i++)
sl1=sl1+1;
for(i=0;st2[i]='\0';i++)
sl2=sl2+1;
if(sl1!=sl2)
{
printf("\nString is not equal.");
}
else
{
for(i=0;st1[i]!='\0' || st2[i]!='\0';i++)
{
if(st1[i]!=st2[i])
{ f=1; }
}
if(f==1)
printf("\nString is not equal.");
else
printf("\nString is equal.");
}
getch();
}

Is This Answer Correct ?    1 Yes 5 No

Post New Answer

More C Interview Questions

why use functions a) writing functions avoids rewriting the same code over and over b) using functions it becomes easier to write programs and keep track of what they are doing c) a & b d) none of the above

0 Answers  


Program to display given 3 integers in ascending order

1 Answers   N Tech,


What is double pointer in c?

0 Answers  


program to find the roots of a quardratic equation

1 Answers  


Program to write some contents into a file using file operations with proper error messages.

2 Answers  






What is the difference between c &c++?

0 Answers  


what is the main use of c where it can use the c

2 Answers   Infosys,


WRITE A PROGRAM TO FIND A REVERSE OF TWO NO

7 Answers  


What is difference between %d and %i in c?

0 Answers  


a way in which a pointer stores the address of a pointer which stores the value of the target value a) reference b) allocation c) multiple indirection d) none

0 Answers  


what is the difference between char * const and const char *?

2 Answers   TCS,


how to write optimum code to divide a 50 digit number with a 25 digit number??

0 Answers   MGM,


Categories