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

Explain the bubble sort algorithm.

0 Answers  


What is function prototype?

0 Answers  


what is Structural oriented language? give some example of this language.....?

1 Answers  


control 50 devices which has 2 states on and off.using bitwise operator.plz answer it its urgent

1 Answers  


What is c system32 taskhostw exe?

0 Answers  






how do you redirect stdout value from a program to a file?

1 Answers  


for(i=0;i=printf("Hello");i++); printf("Hello"); how many times how will be printed?????????

8 Answers  


How can I invoke another program or command and trap its output?

0 Answers  


What are the 4 types of organizational structures?

0 Answers  


In C language what is a 'dangling pointer'?

0 Answers   Accenture,


What are the different types of control structures in programming?

0 Answers  


What are local variables c?

0 Answers  


Categories