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

what is the output on the screen? int n; n=printf("my name is %d",printf("kiran %d",printf("kumar"))); printf("\n %d \n",n);

4 Answers   TCS,


main() { int a=0; if(a=0) printf("Ramco Systems\n"); printf("India\n"); } output?

7 Answers   Ramco,


what type of questions arrive in interview over c programming?

0 Answers  


what is difference between array of characters and string

18 Answers   Accenture, Nest,


Read the following data in two different files File A: aaaaaaaadddddddd bbbbbbbbeeeeeeee ccccccccffffffff File B: 11111111 22222222 33333333 By using the above files print the following output or write it in the Other file as follows aaaaaaaa11111111dddddddd bbbbbbbb22222222eeeeeeee cccccccc33333333ffffffffffff

0 Answers  






Why is a semicolon (;) put at the end of every program statement?

0 Answers  


If errno contains a nonzero number, is there an error?

0 Answers  


Explain what are multibyte characters?

0 Answers  


What happens if a header file is included twice?

0 Answers  


Explain what are preprocessor directives?

0 Answers  


What is the difference between declaring a variable and defining a variable?

0 Answers  


write a program to convert a expression in polish notation (postfix) to inline (normal)

0 Answers   Siemens,


Categories