简体   繁体   中英

C program that reads five strings and only prints strings that begin with the letter 'a'

I'm new to programming and cant figure out what I'm doing wrong.

From the 5 given strings I'm trying to print only strings that begin with the character 'a'.

This is what i have done so far

#include <stdio.h>

int main()
{
    char str[5][100];
    int i;

    for (i = 0; i < 5; i++)
    {
        printf("\nEnter line > ");
        scanf("%s", str[i]);
    }

    for (i = 0; i < 5; i++)
    {
        if (!str[0] == 'a')
        {
            printf("\n%s", str[i]);
        }
    }

    return 0;
}

This part is not working. In my mind its supposed to check the first letter of every string but its not doing that. Instead the program doesn't print out anything when this if statement is there.

if (!str[0] == 'a')

In your if statement, you need to remove the ! character and access the first element of the string i like this:

   if(str[i][0]=='a')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM