user1347895
user1347895

Reputation: 29

characters and strings questions

i have the following question:

Computers are frequently used in check-writing systems, such as payroll and accounts payable applications. Many stories circulate regarding weekly pay- checks being printed (by mistake) for amounts in excess of $1 million. Weird amounts are printed by computerized check-writing systems because of human error and/or machine failure. Systems designers, of course, make every effort to build controls into their systems to prevent erroneous checks from being issued. Another serious problem is the intentional alteration of a check amount by some- one who intends to cash it fraudulently. To prevent a dollar amount from being altered, most computerized check-writing systems employ a technique called check protection.

Checks designed for imprinting by computer contain a fixed number of spaces in which the computer may print an amount. Suppose a paycheck contains nine blank spaces in which the com- puter is supposed to print the amount of a weekly paycheck. If the amount is large, then all nine of those spaces will be filled

for example:

11,230.60 (check amount)
---------
123456789 (position numbers)

On the other hand, if the amount is less than $1000, then several of the spaces will ordinarily be left blank—for example,

99.87
---------
123456789

contains four blank spaces. If a check is printed with blank spaces, it is easier for someone to alter the amount of the check. To prevent a check from being altered, many check-writing systems insert leading asterisks to protect the amount as follows:

****99.87
---------
123456789

Write a program that inputs a dollar amount to be printed on a check and then prints the amount in check-protected format with leading asterisks if necessary. Assume that nine spaces are available for printing an amount.

im not asking anyone to write the code for me or anything, i just need a starting point, we have this huge list of all these different functions, can anyone recommend what function i could use from the string-handling library?

Upvotes: 1

Views: 566

Answers (3)

Vinod
Vinod

Reputation: 4872

  1. Convert amt by using itoa and find length

  2. Now substract length from 9(Max no.of digits) you get the no of asterisks to be printed

  3. Print that no.of asterisks and then your Amt.


int amt,count;
char s[10];
printf("Enter Amt");
scanf("%d",&amt);

/* Store Amount as String */
itoa(amt,s,10);

/* Calculate Count  */
count=9-strlen(s);

/* Print asterisks */
for(int i=0;i<count;i++)
printf("*");

/* Print Amount */
printf("%d",amt);

Upvotes: 0

Roboprog
Roboprog

Reputation: 3144

I won't do your homework for you, but I will encourage you to look at the following functions:

sprintf - formatted output.

Hint: 0 pad the output on the left

strspn / strcspn - count spans of characters in / not in a set of characters

Hint: replace the extra zeroes.

memset - fill memory region with a byte value

I'm assuming you know how to do pointer arithmetic with characters and "strings" in C. If not, study up. I'm also assuming ASCII, rather than unicode. If it's unicode, you can teach me how to do it in C :-)

Upvotes: 2

Wyzard
Wyzard

Reputation: 34563

Think about the relationship between the number of digits to be printed and the number of asterisks that should precede the digits. They should add up to a constant, in this case 9. So if you get the length of the digit string you're going to print, you can determine how many asterisks to print first.

Upvotes: 2

Related Questions