~ols/wordcalc

3fba69f8e0615db64b89fab81e4cc4bf0c15bfea — Oliver Leaver-Smith 2 years ago f42b2ec
The Will-effect
1 files changed, 19 insertions(+), 19 deletions(-)

M wordcalc.c
M wordcalc.c => wordcalc.c +19 -19
@@ 7,35 7,28 @@
#include <string.h>
#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 isValidWord(char word[])
{
    int length = strlen(word);
    int letters = 0;

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

    if (letters == length)
    {
        return 0;
    }
    else
    {
        return 1;
    }
    return 0;

}

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 sumLetters(char word[])
{
    int length = strlen(word);
    int sum = 0;


@@ 50,28 43,30 @@ int sumLetters(char word[])

}

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

void printMessage(char *message)
// Print a message to the console
void printMessage(char *message)
{
    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
int main(int argc, char **argv)
{
    if (argc != 2)
    {
        printMessage("Please provide one word");
        return 1;
    }

    // Validate word passed is less than 100 characters
    char word[100];
    char *argument = argv[1]; 
    if (strlen(argument) > 99)


@@ 79,12 74,17 @@ int main(int argc, char **argv)
        printMessage("Word too long");
        return 1;
    }

    // Convert 'argument' string to 'word' array
    strcpy(word,argument);
    if (validWord(word) != 0)

    if (isValidWord(word) != 0)
    {
        printMessage("Word fails validation, you probably have symbols or numbers in there");
        return 1;
    }
    
    printResult(sumLetters(word));
    
    return 0;
}