write a own function to compare two strings with out using
stringcomparition function?

Answers were Sorted based on User's Feedback



write a own function to compare two strings with out using stringcomparition function?..

Answer / ravikiran p sattigeri

/* str_cmp : Comapares the character strings s1 and s2, and
returns negative, zero or positive if s1 is
lexicographically less than, equal to, or greater than s2.
The value is obtained by subtracting the characters at the
first position where s1 and s2 disagree */

/* return < 0 if s1<s2, 0 if s1==s2, >0 if s1>s2 */
int str_cmp (char *s1, char *s2)
{
while(*s1++ == *s2++) {
if (*s1 == '\0')
return 0;
}
return *S1 - *s2;
}

Is This Answer Correct ?    13 Yes 3 No

write a own function to compare two strings with out using stringcomparition function?..

Answer / geetha

main()
{
char a[10],b[10];
int i,l2,l1;
printf("Enter two strings");
scanf("%s\n",a);
scanf("%s",b);
l1=strlen(a);
l2=strlen(b);
if(l1==l2)
{
for(i=0;i<l1;i++)
{
if(a[i]==b[i])
continue;
else
printf("no match");
break;
}
}
else
printf("no match");
if(i==11)
printf("both strings are same");
}

Is This Answer Correct ?    1 Yes 0 No

write a own function to compare two strings with out using stringcomparition function?..

Answer / vignesh1988i

sorry ... i made a small logical problem in a above posted
answer ... so this program will work out correctly.....

#include<stdio.h>
#include<conio.h>
int xstrcmp(char*,char*);
void main()
{
int c;
char a1[20],a2[20];
printf("enter the two strings :");
gets(a1);
gets(a2);
c=xstrcmp(&a1[0],&a2[0]);
printf("%d",c);
getch();
}
int xstrcmp(char *a,char *a1)
{
if((*a)!=(*a1))
return((*a)-(*a1));
else if((*a)!='\0' || (*a1)!='\0')
{
(a)++;
(a1)++;
xstrcmp(a,a1);
}
else
return 0;
}

Is This Answer Correct ?    2 Yes 2 No

write a own function to compare two strings with out using stringcomparition function?..

Answer / vignesh1988i

#include<stdio.h>
#include<conio.h>
int xstrcmp(char*,char*);
void main()
{
int c;
char a1[20],a2[20];
printf("enter the two strings :");
gets(a1);
gets(a2);
c=xstrcmp(&a1[0],&a2[0]);
printf("%d",c);
getch();
}
int xstrcmp(char *a,char *a1)
{
if((*a)!=(*a1))
return((*a)-(*a1));
else if((*a)!='\0' && (*a1)!='\0')
{
(*a)++;
(*a1)++;
xstrcmp(a,a1);
}
else
return 0;
}

Is This Answer Correct ?    5 Yes 6 No

write a own function to compare two strings with out using stringcomparition function?..

Answer / sasikumar

main()
{
char s1[10],s2[10];
printf("enter two strings:");
scanf("%s%s",&s1[10],&s2[10]);
compare(s1[10],s2[10]);
getch();
}
compare(char st1[10],char st2[10])
{
int l1,l2,i;
l1=strlen(st1[10]);
l2=strlen(str2[10]);

if(l1==l2)
{
for(i=0;i<l1;i++)
{
if(st1[i]==st2[i])
{
if(i==l1)
{
printf("two strings are equal");
}

}
else
goto label:
}
}
else
goto label:
label:printf("not equal");
}

Is This Answer Correct ?    0 Yes 1 No

write a own function to compare two strings with out using stringcomparition function?..

Answer / mallesh

#include<stdio.h>
#include<conio.h>
int xstrcmp(char*,char*);
void main()
{
int c;
char a1[20],a2[20];
printf("Enter the two string's :\n");
gets(a1);
gets(a2);
c=xstrcmp(&a1[0],&a2[0]);
printf("%d",c);
getch();
}
int xstrcmp(char *a,char *a1)
{

while((*a)==(*a1))
{
if((*a=='\0')||(*a1=='\0'))
break;
++(a);
++(a1);
}
return((*a)-(*a1));
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Interview Questions

What is #include in c?

0 Answers  


Can a variable be both constant and volatile?

0 Answers  


create an SINGLE LINKED LISTS and reverse the data in the lists completely

3 Answers  


here is a link to download Let_Us_C_-_Yashwant_Kanetkar

3 Answers   Microsoft,


int x=5; printf("%d%d%d",x,x<<2,x>>2);

2 Answers   TANCET,






What is double pointer?

0 Answers  


simple program for virtual function?

1 Answers  


how to connect oracle in C/C++.

3 Answers   TCS, Wipro,


How can I recover the file name given an open stream?

0 Answers  


Explain how can I pad a string to a known length?

0 Answers  


Explain what is the difference between far and near ?

0 Answers  


Write a C program to perform some of the operation which can be performed using Single linked list

1 Answers   Qualcomm,


Categories