Programming Fundamentals

Information

  • This page contains additional revision exercises for week 07.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • You cannot submit any of these exercises, however autotests may be available for some them (Command included at bottom of each exercise if applicable).

Exercise — individual:
Secret Code

TOP SECRET // COMP1511 ONLY

Write a file secret_code.c which allows you to scan in messages encrypted with Tom's Secret Code, and then print them out (ending in a newline).

Tom's Secret Code works two letters at a time. Given some ciphertext (text that has been encrypted with Tom's Secret Code), take the first two letters. The first letter of the plaintext (unencrypted text) is the letter with the smaller ascii value of those two encrypted letters. For example, if the first two letters of ciphertext were "GD", the first letter of the plaintext would be "D".

To explain the code, the following diagram demonstrates how the code "CZuOMUPP1i5fg112" is decrypted as "COMP1511". In each pair of letters, the one with the lower ascii value was the one that was part of the plaintext.

Cipher Text C Z u O M U P P 1 i 5 f g 1 1 2
ASCII Values 67 90 117 79 77 85 80 80 49 105 53 102 103 49 49 50
Correct Answer C O M P 1 5 1 1

Your program should behave exactly as these examples do:

dcc secret_code.c -o secret_code
./secret_code
abbccddeeffggh
abcdefg 
./secret_code
CZuOMUPP1i5fg112
COMP1511 

Assumptions/Restrictions/Clarifications

  • You should not assume that there will be an even number of inputs. If there is an odd number of characters, you should ignore the last character.
  • You could be given any printable ascii character as input (lowercase letters, uppercase letters, newlines, symbols, etc.)
  • Your program should always print a newline at the end of it's output
  • This exercise is very difficult to solve using fgets. You should solve this using scanf("%c", ...)

Exercise — individual:
Strings Equal

Download strings_equal.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity strings_equal

You've probably noticed that you can't use the comparison operator, ==, to compare strings. So, how do we compare two strings? For this activity, you'll be writing a function to do exactly that:

You may not use string.h in this exercise.

It takes two strings, string1 and string2, and, if they are element-for-element the same, it returns 1, and 0 otherwise. You shouldn't ever read beyond the null-terminator of either string.

Download strings_equal.c here, or copy it to your CSE account using the following command:

cp -n /import/adams/A/cs1511/public_html/26T3/activities/strings_equal/strings_equal.c .

Your task is to add code to this function in strings_equal.c:

// Takes two strings, and if they are the same,
// returns 1, or 0 otherwise.
int strings_equal(char *string1, char *string2) {
    // Your code goes here!
    // Don't forget to return your result.
    return 0;
}

strings_equal.c also contains a simple main function with some simple assert-based tests to help you build your solution:

int main(int argc, char *argv[]) {

    // Some simple assert-based tests.
    // You probably want to write some more.

    // Assert will terminate program if evaluated as false
    assert(strings_equal("", "") == 1);
    assert(strings_equal(" ", "") == 0);
    assert(strings_equal("", " ") == 0);
    assert(strings_equal(" ", " ") == 1);
    assert(strings_equal("\n", "\n") == 1);
    assert(strings_equal("This is 17 bytes.", "") == 0);
    assert(strings_equal("", "This is 17 bytes.") == 0);
    assert(strings_equal("This is 17 bytes.", "This is 17 bytes.") == 1);
    assert(strings_equal("Here are 18 bytes!", "This is 17 bytes.") == 0);

    printf("All tests passed.  You are awesome!\n");

    return 0;
}

Your strings_equal function will be called directly in marking. The main function is only to let you test your strings_equal function

You can add more assert tests to main to test your strings_equal function.

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest strings_equal
Sample solution for strings_equal.c
// String Equality
// A sample solution.

#include <stdio.h>
#include <assert.h>

int strings_equal(char *string1, char *string2);

int main(int argc, char *argv[]) {

    // Some simple assert-based tests.
    // You probably want to write some more.
    assert(strings_equal("", "") == 1);
    assert(strings_equal(" ", "") == 0);
    assert(strings_equal("", " ") == 0);
    assert(strings_equal(" ", " ") == 1);
    assert(strings_equal("\n", "\n") == 1);
    assert(strings_equal("This is 17 bytes.", "") == 0);
    assert(strings_equal("", "This is 17 bytes.") == 0);
    assert(strings_equal("This is 17 bytes.", "This is 17 bytes.") == 1);
    assert(strings_equal("Here are 18 bytes!", "This is 17 bytes.") == 0);

    printf("All tests passed.  You are awesome!\n");

    return 0;
}


// Takes two strings, and if they are the same,
// returns 1, or 0 otherwise.
int strings_equal(char *string1, char *string2) {
    int i = 0;

    // note if string2[i] == '\0', loops terminates too
    while (string1[i] == string2[i] && string1[i] != '\0') {
        i = i + 1;
    }

    // could be just return string1[i] == string2[i];

    if (string1[i] == string2[i]) {
        return 1;
    } else {
        return 0;
    }
}

Exercise — individual:
String to Upper

Download string_to_upper.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity string_to_upper

For this activity, you'll be writing the function string_to_upper. It takes a string and converts it to upper case

string_to_upper.c also contains a simple main function to help you test your solution.

int main(int argc, char *argv[]) {

    char str[] = "Seventeen...  SEVENTEEN, I SAY!";
    string_to_upper(str);
    printf("%s\n", str);
    return 0;
}

Your string_to_upper function will be called directly in marking. The main function is only to let you test your string_to_upper function

int main(int argc, char *argv[]) {

    char str[] = "Seventeen...  SEVENTEEN, I SAY!";
    string_to_upper(str);
    printf("%s\n", str);
    return 0;
}

Your string_to_upper function will be called directly in marking. The main function is only to let you test your string_to_upper function

Here is how string_to_upper.c should behave after you add the correct code to the function string_to_upper:

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest string_to_upper
Sample solution for string_to_upper.c
// Convert a string to uppercase
// string_to_upper.c
//
// This program was written by YOUR-NAME-HERE (zXXXXXXX)
// on INSERT-DATE-HERE

#include <stdio.h>

void string_to_upper(char *buffer);
int uppercase(int c);

int main(int argc, char *argv[]) {

    // NOTE: THIS WON'T WORK:
    // char *str = "Hello!"
    // string_to_upper(str)
    //
    // str only points to a string literal, which it is not legal to change.
    // If you attempt to modify it on Linux you will get a runtime error.
    // Instead, you need to create an array to store the string in, e.g.:
    //
    // char str[] = "Hello!"
    // string_to_upper(str)

    char str[] = "Seventeen...  SEVENTEEN, I SAY!";
    string_to_upper(str);
    printf("%s\n", str);

    return 0;
}

// Convert the characters in `buffer` to upper case
void string_to_upper(char *buffer) {
    int i = 0;
    while (buffer[i] != '\0') {
        buffer[i] = uppercase(buffer[i]);
        i = i + 1;
    }
}

int uppercase(int c) {
    if (c >= 'a' && c <= 'z') {
        return c - 'a' + 'A';
    } else {
        return c;
    }
}

Exercise — individual:
String to Lower

Download string_to_lower.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity string_to_lower

For this activity, you'll be writing the function string_to_lower. It takes a string and converts it to lower case.

string_to_lower.c also contains a simple main function to help you test your solution.

int main(void) {

    char str[MAX_LEN] = "Hi, mY nAmE iS sPonGEbOb sQuArePanTS.";
    string_to_lower(str);
    printf("%s\n", str);

    return 0;
}

Your string_to_lower function will be called directly in marking. The main function is only to let you test your string_to_lower function.

Here is how string_to_lower.c should behave after you add the correct code to the function string_to_lower:

dcc string_to_lower.c -o string_to_lower
./string_to_lower
hi, my name is spongebob squarepants.

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest string_to_lower
Sample solution for string_to_lower.c
// convert a string to lowercase
// a sample solution.

#include <stdio.h>

#define MAX_LEN 1024

void string_to_lower(char *buffer);
int lowercase(int c);

int main(void) {

    // NOTE: THIS WON'T WORK:
    // char *str = "Hello!"
    // string_to_lower(str)
    //
    // str only points to a string literal, which it is not legal to change.
    // If you attempt to modify it on Linux you will get a runtime error.
    // Instead, you need to create an array to store the string in, e.g.:
    //
    // char str[] = "Hello!"
    // string_to_lower(str)

    char str[MAX_LEN] = "Seventeen...  SEVENTEEN, I SAY!";
    string_to_lower(str);
    printf("%s\n", str);

    return 0;
}

// Convert the characters in `buffer` to lower case
void string_to_lower(char *buffer) {
    int i = 0;
    while (buffer[i] != '\0') {
        buffer[i] = lowercase(buffer[i]);
        i = i + 1;
    }
}

int lowercase(int c) {
    if (c >= 'A' && c <= 'Z') {
        return c - 'A' + 'a';
    } else {
        return c;
    }
}

Exercise — individual:
String Manipulation

Download string_manipulation.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity string_manipulation

Your task is to add code to this function in string_manipulation.c:

    // TODO: move code sections into functions
}

Implement the different functions listed in the string_manipulation.c file.

No autotests are provided for this question.

Examples

dcc struct_tutorial.c -o struct_tutorial
./struct_tutorial
Hello, World!
Goodbye
Goodbye is before World.
Sample solution for string_manipulation.c
// Strings
// Some string practice:
//      - declaring & initialising
//      - printing
//      - scanning a string
//      - count vowels and consonants
//      - check string equality (check if 2 strings are the same)
//      - reverse a string
//      - string concatenation (adding a string to the end of another)
//      - title case conversion (every first letter in a word becomes a capital)
//      - count words (words are a series of characters surrounded by spaces)

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

#define MAX_STR_LEN 1024

void count_vowels_consonants(char *string);
int is_vowel(char letter);
void reverse_string(char *string);
void string_concatenation(char *string1, char *string2);
void title_case_conversion(char *string);
void count_words(char *string);

int main(void) {
    // MOST IMPORTANT/USEFUL:

    // Declare & Initialise a String
    char str1[MAX_STR_LEN] = "Hello, World!\n";

    // Print a string
    printf("%s", str1);

    // Scan a string from stdin
    fgets(str1, MAX_STR_LEN, stdin);
    str1[strcspn(str1, "\n")] = '\0';

    // Check if a string is the same as another
    char str2[] = "World";
    if (strcmp(str1, str2) == 0) {
        printf("%s and %s are the same.\n", str1, str2);
    } else if (strcmp(str1, str2) < 0) {
        printf("%s is before %s.\n", str1, str2);
    } else {
        printf("%s is before %s.\n", str2, str1);
    }

    // Copy a string into another
    strcpy(str2, str1);
    printf("str2 is now %s.\n", str2);

    // LESS IMPORTANT/EXTRA PROBLEM SOLVING

    // Count Vowels and Consonants
    count_vowels_consonants(str1);

    // Reverse a String
    reverse_string(str1);

    // String Concatenation
    string_concatenation(str1, "END");
    // Can just use the function strcat
    // strncat(str1, "END");
    printf("%s\n", str1);

    // Title Case Conversion
    title_case_conversion(str1);
    printf("%s\n", str1);

    // Count Words
    count_words(str1);
}

void count_vowels_consonants(char *string) {
    int vowel_count = 0;
    int consonant_count = 0;
    for (int i = 0; string[i] != '\0'; i++) {
        if (is_vowel(string[i])) {
            vowel_count++;
        } else if (isalpha(string[i])) {
            consonant_count++;
        }
    }
    printf("There are %d vowels and %d consonants.\n", vowel_count, consonant_count);
}

int is_vowel(char letter) {
    letter = tolower(letter);
    if (letter == 'a') {
        return 1;
    } else if (letter == 'e') {
        return 1;
    } else if (letter == 'i') {
        return 1;
    } else if (letter == 'o') {
        return 1;
    } else if (letter == 'u') {
        return 1;
    }
    return 0;
}

void reverse_string(char *string) {
    char reversed[MAX_STR_LEN];
    for (int i = 0, j = strlen(string) - 1; j >= 0; i++, j--) {
        reversed[i] = string[j];
    }
    reversed[strlen(string)] = '\0';
    printf("%s\n", reversed);
}

void string_concatenation(char *string1, char *string2) {
    int i = 0;
    while (string1[i] != '\0') {
        i++;
    }
    int j = 0;
    while (j < strlen(string2)) {
        string1[i] = string2[j];
        i++;
        j++;
    }
    string1[i] = '\0';
}

void title_case_conversion(char *string) {
    int prev = 1;
    for (int i = 0; string[i] != '\0'; i++) {
        if (prev) {
            string[i] = toupper(string[i]);
        }
        if (string[i] == ' ') {
            prev = 1;
        } else {
            prev = 0;
        }
    }
}

void count_words(char *string) {
    int prev = 1;
    int word_count = 0;
    for (int i = 0; string[i] != '\0'; i++) {
        if (prev && isalpha(string[i])) {
            word_count++;
        }
        if (string[i] == ' ') {
            prev = 1;
        } else {
            prev = 0;
        }
    }
    printf("There are %d words in this sentence.\n", word_count);
}

Exercise — individual:
String Length

Download string_length.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity string_length

For this activity, you'll be writing the function string_length. It takes a string, and finds its length, excluding the null-terminator.

string_length.c also contains a simple main function with some simple assert-based tests to help you build your solution:

int main(int argc, char *argv[]) {

    // Some simple assert-based tests.
    // You probably want to write some more.

    // Assert will terminate program if evaluated as false
    assert(string_length("") == 0);
    assert(string_length("!") == 1);
    assert(string_length("Hello, world!") == 13);
    assert(string_length("17... seventeen.\n") == 17);

    printf("All tests passed.  You are awesome!\n");

    return 0;
}

Your string_length function will be called directly in marking. The main function is only to let you test your string_length function

You can add more assert tests to main to test your string_length function.

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest string_length
Sample solution for string_length.c
// How long is a (piece of) string?
// A sample solution.

#include <assert.h>
#include <stdio.h>

int string_length(char *string);

int main(int argc, char *argv[]) {

    // Some simple assert-based tests.
    // You probably want to write some more.
    assert(string_length("") == 0);
    assert(string_length("!") == 1);
    assert(string_length("Hello, world!") == 13);
    assert(string_length("17... seventeen.\n") == 17);

    printf("All tests passed.  You are awesome!\n");

    return 0;
}

// Takes a string and finds its length, excluding the null-terminator.
int string_length(char *string) {
    int i = 0;
    while (string[i] != '\0') {
        i = i + 1;
    }
    return i;
}

Exercise — individual:
String Copy

Download string_copy.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity string_copy

For this activity, you'll be writing the function string_copy

It takes a string in the source buffer, and copies it to the destination buffer, which is dest_size elements in size. If there are more characters in source than there is array space in destination, you should stop after you have filled the array. You should always make sure that your function null-terminates the destination array.

string_copy.c also contains a simple main function with to help you test your string_copy function

int main(int argc, char *argv[]) {
    // Declare a buffer.  In this case, we're declaring and using a
    // 64-byte buffer, but this could be any length you like, and in
    // our tests you will be required to handle arrays of any length.
    char buffer[BUFFER_LENGTH] = {0};

    // Copy a string into the buffer ...
    string_copy(buffer, "Seventeen bytes.\n", BUFFER_LENGTH);

    // ... and print it out.  The `%s` format code prints a string.
    printf("<%s>\n", buffer);

    return 0;
}

Your string_copy function will be called directly in marking. The main function is only to let you test your string_copy function

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest string_copy
Sample solution for string_copy.c
// Copy a String
// A sample solution.

#include <stdio.h>

#define BUFFER_LENGTH 64

void string_copy(char *destination, char *source, int destination_size);

int main(int argc, char *argv[]) {
    // Declare a buffer.  In this case, we're declaring and using a
    // 64-byte buffer, but this could be any length you like, and in
    // our tests you will be required to handle arrays of any length.
    char buffer[BUFFER_LENGTH] = {0};

    // Copy a string into the buffer ...
    string_copy(buffer, "Seventeen bytes.\n", BUFFER_LENGTH);

    // ... and print it out.  The `%s` format code prints a string.
    printf("<%s>\n", buffer);

    return 0;
}

// Takes a string in `source`, and copies it to `destination`, which
// is `destSize` elements in size; only copies up to `destSize` bytes.
// Ensures the `destination` array is null-terminated.
void string_copy(char *destination, char *source, int destination_size) {
    int i = 0;

    // stop before last array element so we can add '\0'
    while (i < destination_size - 1 && source[i] != '\0') {
        destination[i] = source[i];
        i = i + 1;
    }
    destination[i] = '\0';
}

Exercise — individual:
String Reverse

Download string_reverse.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity string_reverse

For this activity, you'll be writing the function string_reverse. It takes a string and reverses it in place.

string_reverse.c also contains a simple main function to help you test your function string_reverse

int main(int argc, char *argv[]) {

    char str[] = ".'neetneves' :egassem terces A";
    string_reverse(str);
    printf("%s\n", str);
    return 0;
}

Your string_reverse function will be called directly in marking. The main function is only to let you test your string_reverse function

Here is how string_reverse.c should behave after you add the correct code to the function string_reverse:

dcc string_reverse.c -o string_reverse
./string_reverse
A secret message: 'seventeen'.

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest string_reverse
Sample solution for string_reverse.c
// gnirts a esreveR (Reverse a string)
// A sample solution.

#include <stdio.h>

void string_reverse(char *buffer);
int string_length(char *string);

int main(int argc, char *argv[]) {

    // NOTE: THIS WON'T WORK:
    // char *str = "Hello!"
    // string_reverse(str)
    //
    // str only points to a string literal, which it is not legal to change.
    // If you attempt to modify it on Linux you will get a runtime error.
    // Instead, you need to create an array to store the string in, e.g.:
    //
    // char str[] = "Hello!"
    // string_reverse(str)

    char str[] = ".'neetneves' :egassem terces A";
    string_reverse(str);
    printf("%s\n", str);
    return 0;
}

// Takes a string in `buffer`, and reverses it in-place.
void string_reverse(char *buffer) {
    int length = string_length(buffer);
    int i = 0;
    while (i < length/2) {

        // swap array elements
        char tmp = buffer[i];
        buffer[i] = buffer[length - i - 1];
        buffer[length - i - 1] = tmp;

        i = i + 1;
    }
}

// Takes a string and finds its length, excluding the null-terminator.
int string_length(char *string) {
    int i = 0;
    while (string[i] != '\0') {
        i = i + 1;
    }
    return i;
}

Exercise — individual:
String Manipulation

Download string_manipulation.c here

Or, copy these file(s) to your CSE account using the following command:

1511 fetch-activity string_manipulation

Your task is to add code to this function in string_manipulation.c:

    // TODO: move code sections into functions
}

Implement the different functions listed in the string_manipulation.c file.

No autotests are provided for this question.

Examples

dcc struct_tutorial.c -o struct_tutorial
./struct_tutorial
Hello, World!
Goodbye
Goodbye is before World.
Sample solution for string_manipulation.c
// Strings
// Some string practice:
//      - declaring & initialising
//      - printing
//      - scanning a string
//      - count vowels and consonants
//      - check string equality (check if 2 strings are the same)
//      - reverse a string
//      - string concatenation (adding a string to the end of another)
//      - title case conversion (every first letter in a word becomes a capital)
//      - count words (words are a series of characters surrounded by spaces)

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

#define MAX_STR_LEN 1024

void count_vowels_consonants(char *string);
int is_vowel(char letter);
void reverse_string(char *string);
void string_concatenation(char *string1, char *string2);
void title_case_conversion(char *string);
void count_words(char *string);

int main(void) {
    // MOST IMPORTANT/USEFUL:

    // Declare & Initialise a String
    char str1[MAX_STR_LEN] = "Hello, World!\n";

    // Print a string
    printf("%s", str1);

    // Scan a string from stdin
    fgets(str1, MAX_STR_LEN, stdin);
    str1[strcspn(str1, "\n")] = '\0';

    // Check if a string is the same as another
    char str2[] = "World";
    if (strcmp(str1, str2) == 0) {
        printf("%s and %s are the same.\n", str1, str2);
    } else if (strcmp(str1, str2) < 0) {
        printf("%s is before %s.\n", str1, str2);
    } else {
        printf("%s is before %s.\n", str2, str1);
    }

    // Copy a string into another
    strcpy(str2, str1);
    printf("str2 is now %s.\n", str2);

    // LESS IMPORTANT/EXTRA PROBLEM SOLVING

    // Count Vowels and Consonants
    count_vowels_consonants(str1);

    // Reverse a String
    reverse_string(str1);

    // String Concatenation
    string_concatenation(str1, "END");
    // Can just use the function strcat
    // strncat(str1, "END");
    printf("%s\n", str1);

    // Title Case Conversion
    title_case_conversion(str1);
    printf("%s\n", str1);

    // Count Words
    count_words(str1);
}

void count_vowels_consonants(char *string) {
    int vowel_count = 0;
    int consonant_count = 0;
    for (int i = 0; string[i] != '\0'; i++) {
        if (is_vowel(string[i])) {
            vowel_count++;
        } else if (isalpha(string[i])) {
            consonant_count++;
        }
    }
    printf("There are %d vowels and %d consonants.\n", vowel_count, consonant_count);
}

int is_vowel(char letter) {
    letter = tolower(letter);
    if (letter == 'a') {
        return 1;
    } else if (letter == 'e') {
        return 1;
    } else if (letter == 'i') {
        return 1;
    } else if (letter == 'o') {
        return 1;
    } else if (letter == 'u') {
        return 1;
    }
    return 0;
}

void reverse_string(char *string) {
    char reversed[MAX_STR_LEN];
    for (int i = 0, j = strlen(string) - 1; j >= 0; i++, j--) {
        reversed[i] = string[j];
    }
    reversed[strlen(string)] = '\0';
    printf("%s\n", reversed);
}

void string_concatenation(char *string1, char *string2) {
    int i = 0;
    while (string1[i] != '\0') {
        i++;
    }
    int j = 0;
    while (j < strlen(string2)) {
        string1[i] = string2[j];
        i++;
        j++;
    }
    string1[i] = '\0';
}

void title_case_conversion(char *string) {
    int prev = 1;
    for (int i = 0; string[i] != '\0'; i++) {
        if (prev) {
            string[i] = toupper(string[i]);
        }
        if (string[i] == ' ') {
            prev = 1;
        } else {
            prev = 0;
        }
    }
}

void count_words(char *string) {
    int prev = 1;
    int word_count = 0;
    for (int i = 0; string[i] != '\0'; i++) {
        if (prev && isalpha(string[i])) {
            word_count++;
        }
        if (string[i] == ' ') {
            prev = 1;
        } else {
            prev = 0;
        }
    }
    printf("There are %d words in this sentence.\n", word_count);
}

Exercise — individual:
String Search

Your job is to write a program called string_search.c which lets us count the number of times we see any our of "search terms" in a list of words.

The search terms will be provided to our program via command line arguments. For example:

./string_search hello there

would run the program with 2 search terms - hello and there.

TASK 1: Edit the arguments to your main function so that it can take in command line arguments (aka, the search terms). Try printing out the values of argc and argv and changing the values you type after ./string_search to see what they contain!

TASK 2: Scan in words from standard input until Ctrl-D is pressed.

TASK 3: Count the number of times the search terms appear in the input.

Examples

dcc string_search.c -o string_search
./string_search same
Enter list of words:
same
sand
same
send
shade
same
shadow

There were 3 occurrence(s) in the input.
./string_search many search terms
Enter list of words:
terms
many
same
many
shade
search
research

There were 4 occurrence(s) in the input.

Assumptions/Restrictions/Clarifications

  • You can assume that each word will be no longer than 128 characters long
  • You can assume that there will only be 1 word per line
  • You can assume that the search terms will never have repeats in them

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest string_search
Sample solution for string_search.c
// Simple Regex
// Searches for words in a file that match any of the given substrings.
// Activity written by Paula
// Finished by [Student]

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

#define MAX_WORD_LEN 128
#define MAX_WORDS 10000


int read_words(char words[MAX_WORDS][MAX_WORD_LEN]);
int word_matches(char *word, char *argv[], int n_match);


int main(int argc, char *argv[]) {

    int n_match = argc - 1;

    printf("Enter list of words:\n");
    char words[MAX_WORDS][MAX_WORD_LEN];
    int n_words = read_words(words);


    int count = 0;
    for (int i = 0; i < n_words; i++) {
        if (word_matches(words[i], &argv[1], n_match)) {
            count++;
        }
    }
    printf("There were %d occurances in the input.\n", count);

    return 0;
}

int word_matches(char *word, char *argv[], int n_match) {
    for (int i = 0; i < n_match; i++) {
        if (strcmp(word, argv[i]) == 0) {
            return 1;
        }
    }
    return 0;
}


////////////////////////////////////////////
// DO NOT CHANGE THE FUNCTIONS BELOW HERE //
////////////////////////////////////////////

// removes a trailing newline from a string if it exists.
void remove_newline(char *str) {
    int len = strlen(str);
    if (str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }
}

// reads words from stdin until ctrl-d (assuming 1 word per line)
// stores the words in words[][] without trailing newlines
// returns the number of words found.
int read_words(char words[MAX_WORDS][MAX_WORD_LEN]) {
    int i = 0;
    while (fgets(words[i], MAX_WORD_LEN, stdin) != NULL) {
        remove_newline(words[i]);
        i++;
    }
    return i;
}
Alternative solution for string_search.c
// Simple Regex
// Searches for words in standard input that match any of the given search
// strings. Written by [Student]

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

#define MAX_WORD_LEN 128

void remove_newline(char *str);

int main(int argc, char *argv[]) {
    printf("Enter list of words:\n");

    int total = 0;

    char buf[MAX_WORD_LEN];
    while (fgets(buf, MAX_WORD_LEN, stdin) != NULL) {
        remove_newline(buf);
        for (int i = 1; i < argc; i++) {
            if (strcmp(buf, argv[i]) == 0) {
                total += 1;
            }
        }
    }
    printf("There were %d occurrence(s) in the input.\n", total);

    return 0;
}

void remove_newline(char *str) {
    int len = strlen(str) - 1;
    if (str[len] == '\n') {
        str[len] = '\0';
    }
}