0

I'm pretty new to C; I usually code in C++ before my lecturer told me to code it in C. So how do I compare two strings in if statement in C?

#include <stdio.h>

char name[100],tanya[50],type[100];

int value;

int main()
{

    printf("Enter name: ");
    scanf("%s", name);
    printf("Hello %s", name);
    printf( ", Are you interested in Anime? (y/n)");
    scanf("%s", tanya);
    if (tanya == "y") { // this is the part
        printf("Wow, you're an interesting person %s");
        do {

        } while ();

    } else {
        printf("good day sir.");

    }
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 10
    Use [`strcmp()`](https://en.cppreference.com/w/c/string/byte/strcmp) to compare strings. – 001 Feb 27 '20 at 18:01
  • the problam was : 1. i have no idea how to and everytime i put y, it goes to the else statement – Frank Filler Feb 27 '20 at 18:01
  • 1
    Aside `printf("Wow, you're an interesting person %s");` lacks an agument for `%s` - please enable compiler warnings. – Weather Vane Feb 27 '20 at 18:05
  • 5
    Does this answer your question? [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) – Kamila Szewczyk Feb 27 '20 at 18:08
  • Possibly related https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer – kichik Feb 27 '20 at 18:10

2 Answers2

1
if (tanya == "y")

You do not need to use nor compare strings, when you only want to input a single character like y or n.

Instead use a char object, input a character in it and compare if this character matches y or Y inside the condition of the if statement:

printf(", Are you interested in Anime? (y/n)");

char tanya;
scanf("%c", &tanya);

if (tanya == 'y' || tanya == 'Y')
{....}

If you explicitly want to use strings, you need to compare two strings properly.

In C, one common way to compare two strings is by using the strcmp() function in the header string.h.

In your code it can be used as:

if(strcmp(tanya,"yes") == 0);

to accomplish the comparison and the proof of the if statement to check if both strings are equal.


The whole code shall be like this:

#include <stdio.h>
#include <string.h>

char name[100],tanya[50],type[100];

int value;


int main()
{

    printf("Enter name: ");
    scanf("%s", name);

    printf("Hello %s", name);
    printf( ", Are you interested in Anime? (y/n)");
    scanf("%s", tanya);

    if(strcmp(tanya,"yes") == 0){                         
        printf("Wow, you're an interesting person %s", tanya);
    } 
    else{
        printf("Good day sir.");
    }
    return 0;
}

By the way, it is kind of superficial to kick anybody just because he/she ain´t like animes ;-)

0

To compare strings in c, you can use strcmp() provided in string.h library.

char a[10],b[10];
if( strcmp(a,b) == 0 ) { 
 // .. both are identical
}

this function returns 0 or non zero, you'll get details in the ref.

alternatively, while learning, implement your own compare function, like:

#include <stdio.h>
#include <string.h>

bool isEqual(char* a, char* b) {
    char* c = a, *d = b; 
    while(*c != '\0' && *d != '\0') {

        if(*c != *d) return false;

        c++;
        d++;
    }
    return true;
}

int main() {
    char a[10],b[10];

    strcpy(a,"name");
    strcpy(b,"name");

    if(isEqual(a,b)) {
        printf("%s %s are same\n",a,b);
    }else{
        printf("%s %s are not same\n",a,b);
    }

    return 0;
}

This might help you to think about what's going on under the hood.

some user
  • 1,693
  • 2
  • 14
  • 31