~ols/wordcalc

f42b2ec4b2630c91c9fd1e5aa241b90fdd40d988 — Oliver Leaver-Smith 2 years ago e838b10
Streamline code a little and comment following colleague suggestions
2 files changed, 49 insertions(+), 12 deletions(-)

M tests.sh
M wordcalc.c
M tests.sh => tests.sh +11 -2
@@ 37,7 37,7 @@ fi
### Leaver-Smith should fail

echo -n "Leaver-Smith should fail validation: "
if [[ "$($path_to_application Leaver-Smith)" == "Word \"Leaver-Smith\" fails validation, you probably have symbols or numbers in there" ]] ; then
if [[ "$($path_to_application Leaver-Smith)" == "Word fails validation, you probably have symbols or numbers in there" ]] ; then
    echo "success!"
else
    echo "fail"


@@ 46,7 46,16 @@ fi
### 100 As should fail

echo -n "100 As should fail: "
if [[ "$($path_to_application AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA)" == "Word \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\" too long" ]] ; then
if [[ "$($path_to_application AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA)" == "Word too long" ]] ; then
    echo "success!"
else
    echo "fail"
fi

### No word should fail

echo -n "No word should fail: "
if [[ "$($path_to_application)" == "Please provide one word" ]] ; then
    echo "success!"
else
    echo "fail"

M wordcalc.c => wordcalc.c +38 -10
@@ 8,15 8,18 @@
#include <ctype.h>

int validWord(char word[])
// Count the characters of the word that are alphas and compare to length of word 
// Return 1 if there is a difference in length
{
    int i;
    int length = strlen(word);
    int letters = 0;

    for (i=0; word[i]; i++)
    for (int i=0; i < length; i++)
    {
        if (isalpha(tolower(word[i])) != 0)
        {
            letters++;
        }
    }

    if (letters == length)


@@ 25,38 28,63 @@ int validWord(char word[])
    }
    else
    {
        printf("Word \"%s\" fails validation, you probably have symbols or numbers in there",word);
        return 1;
    }
}
void sumLetters(char word[])

int sumLetters(char word[])
// Loop through each letter in the word, get ASCII representation and -96 to get alphabet position
// Add each letter onto the running total of letters
// Return the sum 
{
    int i;
    int length = strlen(word);
    int sum = 0;

    for(i = 0; i < length; i++)
    for(int i = 0; i < length; i++)
    {
        int letter = tolower(word[i])-96;
        sum = sum + letter;
    }

    printf("%d\n",sum);
    return sum;

}

void printResult(int sum)
// Print the result of sumLetters
{
    printf("%d\n",sum);
}

void printMessage(char *message)
// Print a message to the console
{
    printf("%s",message);
}
int main(int argc, char **argv)
// Validate number of aguments
// Validate word passed is less than 99 chars
// Validate all characters in word are letters
// Print result to console
{
    char word[100] = "abcd";
    if (argc != 2)
    {
        printMessage("Please provide one word");
        return 1;
    }
    char word[100];
    char *argument = argv[1]; 
    if (strlen(argument) > 99)
    {
        printf("Word \"%s\" too long",argument);
        printMessage("Word too long");
        return 1;
    }
    strcpy(word,argument);
    if (validWord(word) != 0)
    {
        printMessage("Word fails validation, you probably have symbols or numbers in there");
        return 1;
    sumLetters(word);
    }
    printResult(sumLetters(word));
    return 0;
}