// A program to determine if a word rhymes with
// a line of a song

// Reads a line on the command line
// compares the input string against the last word
// of the command line.
// Makes a guess at how well they rhyme

// Marc Chee (cs1511@cse.unsw.edu.au), October 2020

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

#define MAX_LENGTH 100

int check_char(int input_char, char *word);
double check_word(char *word1, char *word2);

int main(int argc, char *argv[]) {
    if (argc <= 1) {
        // no extra arguments are on the command line
        printf("There was nothing to rhyme with!\n");
        return 1;
    } 
    
    // Some code to show command line arguments working
    printf("There were %d words on the command line.\n", argc);
    printf("The name of the program is: %s ", argv[0]);
    printf("The rest of the command line arguments are: ");
    int i = 1;
    while (i < argc) {
        fputs(argv[i], stdout);
        putchar(' ');
        i++;
    }
    
    // at this point, we assume that there's something in argv[1]
    // and possibly more
    
    // final element of argv will always be: argv[argc - 1]
    
 /*   // Take input from the user (single character)
    int input_char = EOF;
    printf("Please enter a character: ");
    input_char = getchar();
    
    if (input_char == EOF) {
        // if this is still EOF, then we haven't read a character properly
        printf("Couldn't read a character.\n");
        return 2;
    }
    
    // Check whether the character is in the final word of the
    // command line arguments
    int found_char = check_char(input_char, argv[argc - 1]);
    
    if (found_char >= 0) {
        printf("Found the letter %c at index %d.\n", input_char, found_char);
    } else {
        printf("Did not find character.\n");
    }
 */
 
    // Take in a string as input
    printf("Please type in a word to rhyme: ");
    char input_line[MAX_LENGTH];
    if (fgets(input_line, MAX_LENGTH, stdin) == NULL) {
        // fgets failed to read
        printf("Couldn't read input string.\n");
        return 2;
    }
    
    double rhyme_score = check_word(input_line, argv[argc - 1]);
    printf("My rhyme score for these words is: %lf.\n", rhyme_score);   
    
    return 0;
}

// checks if input_char is in word
// returns the last index that it finds input_char at
// or returns -1 if it can't find input_char
int check_char(int input_char, char *word) {
    // some testing to see exactly what characters we're testing
    printf("Checking %c against %s.\n", input_char, word);
    int found_letter = -1;
    
    int i = 0;
    while (word[i] != '\0') {
        if (word[i] == input_char) {
            // we've found a match
            found_letter = i;
        }
        i++;
    }
    
    return found_letter;
}

// Checks for how many letters in word1 appear in word2
// Returns the fraction of these letters that it finds
double check_word(char *word1, char *word2) {
    int matches = 0;
    int i = 0;
    while (word1[i] != '\0') {
        if (check_char(word1[i], word2) >= 0) {
            matches++;
        }
        i++;
    }
    
    return (matches * 1.0)/strlen(word1);
}






Resource created Friday 16 October 2020, 01:34:20 PM.

file: rhymer.c


Back to top

COMP1511 20T3 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G