#include <stdio.h>
#include <string.h>
int main() {
char str1[20];
char *str2;
printf("enter string \n"); **// using %c input**
scanf("%c",str1);
printf(" string 1 is %s \n",str1);
printf("enter string 2 \n");
scanf("%s",*str2); //using %s input
printf(" string 1 and 2 is %c and %s \n",str1,str2);**strong text**
int a=strcmp(str1,str2); //**comparing both**
printf("%d",a);
return 0;
}
took input from user using %c and %s then used strcmp for comparing the equality of the strings
%c
reads one character and doesn't add a terminating null-character, so you have to add that to use the data as string.str2
before reading something there.%s
in scanf()
requires a pointer char*
, so str2
should be passed instead of *str2
.%c
in printf()
requires int
, not char*
, so you have to deference the pointer (automatically converted from the array).Try this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str1[20];
char *str2;
printf("enter string \n"); // **using %c input**
scanf("%c",str1);
str1[1] = '\0'; // add terminating null-charachter
printf(" string 1 is %s \n",str1);
str2 = malloc(102400); // allocate buffer
if (str2 == NULL) return 1; // check if allocation is successful
printf("enter string 2 \n");
// pass correct thing
scanf("%s",str2); //using %s input
printf(" string 1 and 2 is %c and %s \n",*str1,str2); // pass correct thing for %c
int a=strcmp(str1,str2); //**comparing both**
printf("%d",a);
free(str2); // free the allocated buffer
return 0;
}