-
What should I do????
I used these codes to concatenate and compare the strings, but couldnt succeed. Please let me know if I m making any mistake. Thank you
void concatenate_string(void) {
char userstrings[5][80];
int x;
int y;
printf("Which two strings would you like to concatenate\?\n");
scanf("%d", &x);
scanf("%y", &y);
strncat(userstrings[x],userstrings[y],80);
fputs("The two strings you concatenated are :\n",stdout);
printf("%s%s", userstrings[x],userstrings[y]);
return;
}
void compare_string (void) {
char userstrings[5][80];
int x;
int y;
printf("Which two strings would you like to compare\?\n");
scanf("%d", &x);
scanf("%d", &y);
fputs("The two strings you compared are :\n",stdout);
printf("%s", strncmp(userstrings[x],userstrings[y],80));
if (userstrings[x]!=userstrings[y]){
printf("%d\n",y");
}
else{
printf("%d\n",y");
}
return;
}
-
>scanf("%y", &y);
%y is not a scanf flag.
>printf("%s%s", userstrings[x],userstrings[y]);
userstrings[x] holds the concatenated strings, so that's all you have to print.
>printf("%s", strncmp(userstrings[x],userstrings[y],80));
This will print 0, 1, or -1, which is an integer, not a string. But then you want to use that value again in the test, a better way would be:
Code:
printf ( "The two strings you compared are :\n%s, %s",
userstrings[x], userstrings[y] );
if ( strncmp ( userstrings[x], userstrings[y], 80 ) != 0 )
/* Not equal */
else
/* Equal */
-Prelude
-
problem
I want it to print 1 if the two strings are not equal and 0 if they are not. Pls help
-
I want it to print 1 if the two strings are not equal and 0 if they are not.
Code:
printf ("%d\n", (strncmp (userstrings[x],userstrings[y],80) != 0) ? 1: 0);
-
sorry
mistyped before,
here is the question
I want it to print 1 if the two strings are not equal and 0 if they are
-
>> printf ("%d\n",
>> (strncmp (userstrings[x],userstrings[y],80) != 0) ? 1: 0);
>
>
> I want it to print 1 if the two strings are not equal and 0 if they
> are
Do you REALLY need help with this? Change the final 1 to a 0, and the 0 to a 1. Or hell, just do this:
printf("%d\n", !!strcmp( userstrings[x],userstrings[y] ) );
Quzah.