0
#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

  • ```scanf("%c",str1)``` will read one char from a keyboard, also you can't assign a string to a ```char *var```. You might want to look for malloc. – alex01011 Nov 22 '20 at 01:51

2 Answers2

1
  • %c reads one character and doesn't add a terminating null-character, so you have to add that to use the data as string.
  • Buffer must be allocated and assigned to 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; 
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • enter string rorr string 1 is r enter string 2 string 1 and 2 is r and orr 1 – Rutvikk Walde Nov 22 '20 at 02:06
  • it is not letting me put second string – Rutvikk Walde Nov 22 '20 at 02:08
  • @RutvikkWalde `%c` is for reading one character. Enter one-character string for string 1. – MikeCAT Nov 22 '20 at 02:09
  • but i want to write a srting /word for both string 1 and string 2 – Rutvikk Walde Nov 22 '20 at 02:14
  • 1
    @RutvikkWalde You should `%s` to read strings, not `%c`. – MikeCAT Nov 22 '20 at 02:15
  • @RutvikkWalde: Although you can use `%9c` to read 9 characters, it will read exactly 9 characters, regardless of blanks, newlines, etc. Further, it won't null-terminate the data; you will have a byte array, not a string. (More accurately, you might accidentally have a string, but that would be because the array already had a null byte at `array[9]` before the input and not because `scanf()` added a null byte.) So, substantively, the answer to "is there any way to read a string using `%c`?" is "No — not easily or reliably". – Jonathan Leffler Nov 22 '20 at 02:19
  • @RutvikkWalde Yes, read characters one-by-one and store them manually in an array. But this looks like [The XY Problem](http://xyproblem.info/). Why do you stick to `%c`? – MikeCAT Nov 22 '20 at 02:20
  • @MikeCAT my professor told us this code #include #include int main() { char st1[34]; char st2[34]; char c; int i=0; printf("enter string 1 \n"); scanf("%s",st1); printf("enter string 2 \n"); while(c!='\0'){ fflush(stdin); scanf("%c",&c); st2[i] =c; i++; } – Rutvikk Walde Nov 22 '20 at 02:32
  • st2[i+1]='\0'; printf("string 1 is %s \n",st1); printf("string 2 is %s \n",st2); printf("strcmp for these stringsd is %d \n",strcmp(st1,st2)); return 0; } – Rutvikk Walde Nov 22 '20 at 02:33
  • @JonathanLeffler can you please verify the above code – Rutvikk Walde Nov 22 '20 at 02:34
  • @RutvikkWalde Your code is wrong. 1. First `while(c!='\0'){` invokes *undefined behavior* by using value of uninitialized non-static local variable, which is indeteminate. 2. `fflush(stdin);` [invokes *undefined behavior*](https://stackoverflow.com/questions/2979209/using-fflushstdin). 3. `st2[i+1]='\0';` contains off-by-one error. – MikeCAT Nov 22 '20 at 02:36
0
#include<stdio.h>
#include<string.h>
int main()
{
char str1[50];
printf("Enter str1:\n");
scanf("%s", str1);   //Using %s input 

char str2[50];
char c;
int i  = 0;
printf("Enter str2:\n");
while(c != '\n')
{
fflush(stdin);  
scanf("%c", &c);  //Using %c input
str2[i] = c;
i++;
}
str2[i-1] = '\0'; 
printf("Str1 is: %s\n", str1);
printf("Str2 is: %s\n", str2);
printf("When comparing str1 and str2 the value it returns is %d", 
strcmp(str1,str2));
return 0;
}

NOTE: When giving the input to string 2 enter the values character by character. for eg:- Enter str2:

  • w
  • o
  • r
  • l
  • d

And, after you've completed your word, press enter two times.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83