Write a program to compare two strings without using the
strcmp() function
Answer Posted / yathish m yadav
#include<conio.h>
int strcmp(char *,char *);
char a[10],b[10];
void main()
{
int c;
printf("enter the first string\n");
scanf("%s",a);
printf("enter the second string\n");
scanf("%s",b);
c=strcmp(a,b);
switch(c)
{
case 1: printf("first string is larger then second");
break;
case 2: printf("second is greater than first");
break;
case 3: printf("not equal");
break;
case 4: printf("the strings are equal");
break;
}
getch();
}
int strcmp(char *p,char *q)
{
int m,n;
m=strlen(p);
n=strlen(q);
if(m>n){
return (1);}
else if(n>m){
return (2);}
for(i=0;i<m;i++)
{
if(p[i]!=q[i])
return(3);
}
return(4);
}
| Is This Answer Correct ? | 4 Yes | 10 No |
Post New Answer View All Answers
How can you call a function, given its name as a string?
What is pointer to pointer in c language?
Explain with the aid of an example why arrays of structures don’t provide an efficient representation when it comes to adding and deleting records internal to the array.
Is c still used?
What does %p mean?
What is self-referential structure in c programming?
console I/O functions means a) the I/O operations done on disk b) the I/O operations done in all parts c) the input given through keyboard is displayed VDU screen d) none of the above
Can we assign string to char pointer?
How is null defined in c?
What is the stack in c?
Is there a way to compare two structure variables?
the portion of a computer program within which the definition of the variable remains unchanged a) mode b) module c) scope d) none
Write a code to generate divisors of an integer?
what are the 10 different models of writing an addition program in C language?
What is far pointer in c?