Ejemplo n.º 1
0
void test_strings()
{
      // test unique
      char str1[] = "gurkans";
      char str2[] = "bananarama";
      assert(has_unique_chars(str1) && !has_unique_chars(str2));
      reverse_string(str1); reverse_string(str1);

      // test are permutations
      assert(are_permutations("snbba","snabb") && !are_permutations("snabb", "snbbb") &&
            !are_permutations("snabb", "sbnb"));

      // Test compression
      char* str3 = "katt";
      char* str4 = "kattttttttttttttttis";
      assert(compressed(str3) == str3 && strlen(compressed(str4)) < strlen(str4));

      // Test substrings
      assert(issubstr("katt", "srkatten") && issubstr("katt", "katt") &&
            !issubstr("katt", "kat") && issubstr("katt", "katten") &&
            issubstr("katten", "katten"));

      // Test rotation
      assert(is_rotated("waterbottle", "erbottlewat") &&
            !is_rotated("waterbottle", "bottleerwat") &&
            is_rotated("waterbottle", "bottlewater"));

}
Ejemplo n.º 2
0
void find(FILE *file) {
    int c = fgetc(file);
    while (c != EOF && isspace(c)) {
        c = fgetc(file);
    }
    ungetc(c, file);
    char *token;
    if (isalpha(c)) {
        token = getname(file);
    } else {
        token = getphone(file);
    }
    if (token == NULL){
        printf("Unable to find");
        fflush(stdout);
        return;
    }
    int i = 0;
    int found = 0;
    for (i = 0; i < book.length; i++) {
        if (issubstr(book.people[i].name, token) || (!strcmp(token, book.people[i].phone))) {
            printf("%d %s %s\n", book.people[i].id, book.people[i].name, book.people[i].phone);
            found = 1;
        }
    }
    if (!found)
        printf("couldn't find");
    fflush(stdout);
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: mamambo/cpp2s
void find() {
    char *query = getword(stdin, 2);
    if (query != NULL) {
        int found = 0;
        for (int i = 0; i < book.length; i++) {
            if ((isdigit(query[0]) && !strcmp(book.contacts[i].phone, query)) ||
                (isalpha(query[0]) && issubstr(book.contacts[i].name, query))) {
                printf("%d %s %s\n", book.contacts[i].id, book.contacts[i].name, book.contacts[i].phone);
                found = 1;
            }
        }
        if (!found) {
            printf("Contact not found.\n");
        }
    } else {
        printf("Search failed.\n");
    }
    free(query);
}