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 language is lisp written in?

0 Answers  


What is advantage of pointer in c?

0 Answers  


What are preprocessor directives in c?

0 Answers  


sir i wanted to know how we wap in c to add numbers without using arithmetic operator in which digits are entered by user?

2 Answers  


Does c have enums?

0 Answers  






What are the preprocessor categories?

0 Answers  


What is the right way to use errno?

0 Answers  


What is array in c with example?

0 Answers  


The statement, int(*x[]) () what does in indicate?

0 Answers  


How do I create a directory? How do I remove a directory (and its contents)?

0 Answers  


WHAT IS MEANT BY LIFE?

2 Answers  


What is variable declaration and definition in c?

0 Answers  


Categories