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
Why is structure padding done in c?
a c code by using memory allocation for add ,multiply of sprase matrixes
What is the use of function in c?
Where are some collections of useful code fragments and examples?
One of the Institutes contains 5 student groups. Every group contains 4 students. Institute wants to store student group’s details in array. Group should contain group member’s details (name and registration number and age), project name, and mark of the group.
What is #define in c?
What is calloc malloc realloc in c?
What are type modifiers in c?
int i=10; printf("%d %d %d", i, i=20, i);
All technical questions
Why do we use int main instead of void main in c?
What do you mean by dynamic memory allocation in c?
how many key words availabel in c a) 28 b) 31 c) 32
What is the difference between the local variable and global variable in c?
What is the difference between test design and test case design?