This C program compares two words entered via command-line arguments and calculates a score for each word. The program determines the winner based on the higher score or declares "Play again!" for a tie.
Table of Contents 📖
Modify your program from project 2, task #2, to accept command-line arguments with single words instead of a sequence of words. The program should calculate the score for each word and compare them to determine the winner or prompt the user to "Play again!" in case of a tie.
- The program should accept exactly two words as command-line arguments, excluding the program name.
- If the number of arguments is not 3 (including the program name), print "invalid number of arguments".
- If the number of arguments is correct, the program will print either "John wins!" or "Play again!" depending on the score comparison of the two words.
- If the number of arguments is incorrect, the program will print "invalid number of arguments".
./a.out
Output:
invalid number of arguments
./a.out be bee
Output:
John wins!
./a.out peach cheap
Output:
Play again!
./a.out no hi
Output:
Play again!
Explanation:
- In Example #1, the program was called without arguments, so it prints "invalid number of arguments".
- In Example #2, the words "be" and "bee" result in "John wins!" based on their calculated scores.
- In Example #3, the words "peach" and "cheap" have the same score, so the program outputs "Play again!".
- In Example #4, the words "no" and "hi" have the same score, resulting in "Play again!".
- The program must include the function:
int calculate_score(char *word);
- This function calculates the score of a word, which could be based on letter positions, ASCII values, or any other scoring logic.
- The function must use pointer arithmetic (not array subscripting) to visit array elements.
- The program must check if the number of arguments is 3 (including the program name). If not, it should print "invalid number of arguments".
1. Compile the Program:
2. Run the Program:
3. Input Required:
- Two words entered as command-line arguments.