user3255517 user3255517 - 4 months ago 7
C Question

How to use the character array to identify a string

For my class we use char arrays for strings. If I was to use an

if else
statement, would something like this work if I had it modified to do so?

I know an array like this would make every character broken down to simple letters. And to use an
if else
statement I have to go like array[1] == 'H' and so on.

Is there a way to modify the code below to spit out the information I want if I type up "Alas". Right now, it only goes to the
else
part.

int main()
{
char s[10];

printf("Yo, this is a string: ");
gets_s(s);

if (s == "Alas")
{
printf("B ");
}
else
{
printf("A");
}

system("pause");
}

Answer

Something like:

int main()
{
    char s[10];

    printf("Yo, this is a string: ");
    gets_s(s);

    if (strcmp(s, "Alas") == 0)
    {
        printf("B ");
    }
    else
    {
        printf("A");
    }

    system("pause");
}