• Home
  • Blog
  • C Programming Metal Program Design

C Programming Metal Program Design

0 comments

1. Suppose you are given a string s1, such as “psg”, representing the types of precious metal bars. Each character represents one type of metal bar, for example,‘p’ for platinum bar,‘s’ for silver bar, and ‘g’ for gold bar.

Another string s2 is given representing the bars you have, such as “abc”. Each character in s2 is a type of metal bar you have, for example,‘a’ for aluminum, ‘b’ for “brass”, ‘c’ for “copper”.

Write a program to calculate how many of the metal bars you have in s2 are in s1. Letters are case sensitive, so ais considered a different type of metal barfromA.

Example input/output:

Enter s1: psg

Enter s2: abc

Output: 0


Enter s1: psg

Enter s2: ppbgc

Output: 3

1)Name your program metal.c.

2)Your program should include the following function:

int count(char *s1, char *s2);

The count function expects s1 and s2 to be strings. The function returns the number of characters in s2 are in s1. The count function should use pointer arithmetic (instead of array subscribing). In other words, eliminate the loop index variables and all use of the [] operator in the function.

3)Assume s1 and s2 have no more than 1000 characters.

4)String library functions areNOTallowedfor this program. If you use a string library function, you will not receive the credit for the count function part of the program.

5)To read a line of text, use the read_linefunction (the pointer version) in the lecture notes.



2. Commandline arguments

the word is a command-line argument. A word as a command-line argument is valid if all characters of the word are alphabetic letters and one of the following conditions holds:

1. All letters are capitals, like “UCLA”,

2. All letters are not capitals, like “program”.

Example input/output:

./a.out fall

Output: valid

./a.out 8littlepigs

Output: invalid

./a.out

Output: Incorrect number of arguments. Usage: ./a.out word

./a.out spring fall

Output: Incorrect number of arguments. Usage: ./a.out word

1)Name your program command_word.c.

2)The program should include the following function:

int validate(char *word);

The function expects “word” to point to a string containing the wordto be validated. The function returns 1 if word is valid, and 0 otherwise.The function should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function.

3)The program shouldalsocheck if the correct number of arguments are entered on the command line. If an incorrect number of arguments are entered, the program should display a message.

4)The main function displays the output.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}