Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8.34 (Writing the Word Equivalent of a Check Amount) Continuing the discussion o

ID: 3705361 • Letter: 8

Question

8.34 (Writing the Word Equivalent of a Check Amount) Continuing the discussion of the previ- ous exercise, we reiterate the importance of designing check-writing systems to prevent alteration of check amounts. One common security method requires that the check amount be both written in numbers and "spelled out" in words. Even if someone is able to alter the numerical amount of the check, it's extremely difficult to change the amount in words. Write a program that inputs a numer- ic check amount and writes the word equivalent of the amount. For example, the amount 52.48 should be written as FIFTY TWO and 43/100

Explanation / Answer

Writing The Word Euivalent of a Check Amount

/* Program to print a given number in words. The program handles
numbers from 0 to 9999 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* A function that prints given number in words */
void EquivalentWords(char *num)
{
int len = strlen(num); // Get number of digits in given number

/* Base cases */
if (len == 0) {
fprintf(stderr, "empty string ");
return;
}
if (len > 4) {
fprintf(stderr, "Length more than 4 is not supported ");
return;
}

/* The first string is not used, it is to make array indexing simple */
char *onedigit[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};

/* The first string is not used, it is to make array indexing simple */
char *double_digit[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

/* The first two string are not used, they are to make array indexing simple*/
char *multiple_tens[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};

char *powertens[] = {"hundred", "thousand"};

/* Used for debugging purpose only */
printf(" %s: ", num);

/* For single digit number */
if (len == 1) {
printf("%s ", onedigit[*num - '0']);
return;
}

/* Iterate while num is not '' */
while (*num != '') {

/* Code path for first 2 digits */
if (len >= 3) {
if (*num -'0' != 0) {
printf("%s ", onedigit[*num - '0']);
printf("%s ", powertens[len-3]); // here len can be 3 or 4
}
--len;
}

/* Code path for last 2 digits */
else {
/* Need to explicitly handle 10-19. Sum of the two digits is
used as index of "double_digit" array of strings */
if (*num == '1') {
int sum = *num - '0' + *(num + 1)- '0';
printf("%s ", double_digit[sum]);
return;
}

/* Need to explicitely handle 20 */
else if (*num == '2' && *(num + 1) == '0') {
printf("twenty ");
return;
}

/* Rest of the two digit numbers i.e., 21 to 99 */
else {
int i = *num - '0';
printf("%s ", i? multiple_tens[i]: "");
++num;
if (*num != '0')
printf("%s ", onedigit[*num - '0']);
}
}
++num;
}
}

/* Driver program to test above function */
int main(void)
{
EquivalentWords("9923");
EquivalentWords("523");
EquivalentWords("89");
EquivalentWords("8989");

return 0;
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote