CS50_week3 Algorithms

2024-09-06 15:31:50 浏览数 (2)

代码语言:c复制
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    string strings[] = {"battleship", "boot", "cannon", "iron", "thimble", "top hat"};

    string s = get_string("String: ");

    for (int i = 0; i < 6; i  )
    {
        if (strings[i] == s)
        {
            printf("Foundn");
            return 0;
        }
    }
    printf("Not Foundn");
    return 1;
}

不能用string[i] == s来比较string不能用string[i] == s来比较string
代码语言:c复制
#include <cs50.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
    string name;
    string number;
}
person;


int main(void)
{
    person people[3];

    people[0].name = "Carter";
    people[0].number = " 1-617-495-1000";

    people[1].name = "David";
    people[1].number = " 1-617-495-1000";

    people[2].name = "John";
    people[2].number = " 1-949-468-2750";

    string name = get_string("Name: ");

    for (int i = 0; i < 3; i  )
    {
        if (strcmp(people[i].name, name) == 0)
        {
            printf("Found %sn", people[i].number);

            return 0;
        }
    }
    printf("Not Foundn");
    return 1;
}

recursion

0 人点赞