I am writing a program for comparing two strings without using strcmp(). But, am unable to get my desired result. Here is the code for my program.
#include<stdio.h>
int main(int argc, char const *argv[]) {
int i,j;
char a[90],b[90];
printf("Enter the first string:");
scanf("%s", &a[90]);
printf("Enter the second string:");
scanf("%s", &b[90]);
for ( i = 0; a[i] != '\0' && b[i] != '\0'; i++) {
if (a[i] == b[i]) {
/* code */
printf("Equal %d \n", a[i]-b[i]);
continue;
} if (a[i] > b[i]) {
/* code */
printf("ai is big %d \n", a[i]-b[i]);
break;
}
if (a[i] < b[i]) {
/* code */
printf("%d bi is the biggest \n", a[i]-b[i]);
break;
}
}
return 0;
}
When I execute the program in my terminal the compiler takes the input strings and then stops. I tried it a lot but am unable to figure it out. Can anyone help me out...!
The name of your first array is a
, not a[90]
.
Similarly, the name of your second array is b
, not b[90]
.
The expressions a[90]
and b[90]
name one element after the end of a
and b
.
So, by writing &a[90]
and &b[90]
you are instructing scanf
to write just after each array, which is very bad and wrong.
You probably meant &a[0]
and &b[0]
?
However, scanf
is very dangerous and you should not be using it at all.