Write a c program to calculate the total running time for a


[1] In C you can access the command-line arguments by using argc and argv. Since arrays in C don't "know" their own size, the argc parameter to main() is required to know the number of command-line arguments. (Note that the name the program was called by is put in argv[0], so argc is always at least 1.)

In main(), argv is declared as char * argv[]?, which is an array of strings. We haven't learned much about strings yet, but you have learned that a string in C is just a block of consecutive memory locations (like an array), with the characters of the string in consecutive memory locations, followed by the all-important ASCII NUL character ("\0"). So the statement printf("My name is %s\n", argv[0]); will print out the name of the program. (Try writing a six-line (or so) C program to try this out!)

For this problem, your program shall do the following for each command line argument (except argv[0]):

(i) output the argument;
(ii) output the reverse of the argument (see examples below);
(iii) output "palindrome" if the argument is a palindrome (see below), otherwise output "not a palindrome".

A palindrome is a string which reads the same forward as backward. For example, "abba", "zzz", "bob" and "123 f 321" are all palindromes, while "xyzzy" and "ergo" are not. (Note that some people ignore case and punctuation, so for them "Madam, I'm Adam." would be a palindrome (indeed, the first ever palindrome), and they would also recognize "A dog, a plan, a canal: pagoda." as a palindrome. But for this question we shall use the stricter definition.)

Here is a sample run of your program:

$ a4p1 Bob bob 'a qweewq a' 123
/Bob/ reversed is /boB/: not a palindrome
/bob/ reversed is /bob/: palindrome
/a qweewq a/ reversed is /a qweewq a/: palindrome
/123/ reversed is /321/: not a palindrome
$

Your program must define a palindrome test as a function. Specifically, your function must have this signature:

int is_a_palindrome(const char * string)

You function returns 0 ("false") if the string is not a palindrome and a non-zero number ("true")

if the string is a palindrome. (We shall talk about the "const" part in class soon, for now just use it.) This function should not do any output; it should just test the string and return a true/false indication.

You can choose to either create the reversed string or to just output the string in reverse. Contemplate the pros and cons of this while designing your program.

To test your program, run it a number of times on different arguments and different numbers of arguments. As in the example above, if you want to create a command-line argument with embedded white space, you can do so by surrounding the whole argument with single quotes.
(Using double quotes also works to create arguments with embedded white space, but the shell may modify things inside double-quoted strings. Compare the result of "echo 'PATH is $PATH'" to "echo "PATH is $PATH"".)

NOTE: refer to the style guide to see what documentation is expected for functions.

[2] Searching for strings is an extremely common operation in a lot of programs. For this problem you will write a program which accepts two strings as command-line arguments and output some information as described below.

Consider the following text (written by a famous mathematician/logician)
There was one who was famed for the number of things
He forgot when he entered the ship:
His umbrella, his watch, all his jewels and rings,
And the clothes he had bought for the trip.

(I've written this on four lines, but it is really one string with a "\n" character following each of the lines.) Now suppose our search string is "forgotten". In this case, the word we want is not in the text, but various prefixes of the string are. Your program should find the rightmost occurrence of the search string, if it exists, and otherwise of all instances of the longest prefix of the search string in the text, it should find the rightmost one.

In either case, your program then outputs two numbers, as shown below in the examples. The first number is the number of characters of the search string which match, and the second number is how many characters from the right end of the text to go backwards (to find the match).

For example, the prefix "forgot" (of "forgotten") is found 128 characters back from the end of the string, and the length of this longest matched prefix is 6. Thus your program will output "6;128". Here are some examples of how your program should operate. You should include these samples, as well as some of your own creation, when you create your script file.

$ a4p2 'twas brillig' 'was not'
4;11
$ a4p2 'aaron aardvark' 'aaabbb'
2;8
$ a4p2 'aaron aardvark' 'qwerty'
0;q
$ a4p2 hicdefghi hi
2;2
$ a4p2 'A two-line
text string' inky
2;3

You will notice that if there is no (non-empty) prefix of the search string in the text that the program outputs 0 for the matched length (as expected) but instead of outputting the distance from the end, it outputs the first character of the search string. Your program must do this as well.

What other unusual cases should your program deal with? If there are any that you can think of, the comment at the top of your program should describe these cases and what your program does with them. Your program should never "blow up".

Finally, notice that by surrounding the text and search string with single quotes, the spaces become part of their respective command-line argument, rather than acting as argument separators.

Similarly, an embedded newline character becomes part of the argument when enclosed in quotes, rather than completing the command.

Think carefully about what tests you should show to the marker. Try to make every test count!

That is, try to avoid having two or more test cases which have conceptually similar inputs which don't "exercise" different things in your program.

[3] Write a C program to calculate the total running time for a collection of songs. The user enters the running times in the form 5:08 (five minutes and eight seconds), and enters 0:00 to indicate the end of input. (It is also valid for the data to run to the end of the file, with no "0:00" sentinel.) The program then prints (a) how many valid songs times there were, and (b) the total running time in minutes and seconds. (Do I need to point out that there are 60 seconds in a minute?)

If the user enters an invalid running time (in particular, if the number of seconds is not in the range 0 to 59), the input is rejected, the input line is cleared (chars are discarded to the next '\n'), an error message is output, and the program continues execution. You should think about what other sorts of input is invalid for this problem. If the user enters three invalid running times in a row, the program exits with a failure return code. In this latter case no output is produced other than error messages.

The user might optionally put the name of the song (or other textual information) on the same line as the time. Your program must allow this.

Here are two sample runs. Your output doesn't have to look exactly like this, but your output shouldn't be confusing to another person using your program.

$ a4p3
Enter a sequence of times, end with 0:00 or EOF
3:31
5:77
Invalid song time
2:12
4:20 Bob Lob sings "Hob Nob"
0:00
There were 3 valid song times.
The total time is 10:03.
$
$
$ a4p3
Enter a sequence of times, end with 0:00 or EOF
3:31
-5:33
Invalid song time
1:88
Invalid song time
X:01

Three invalid song times in a row; I'm quitting!

Do as much "reasonable" error checking as you can do, without going overboard (no one is likely to die if your song times don't add up, but that is not a license to be lazy or sloppy). So feel free to use scanf() to read in the times, but perform sensible checks as possible.

As one of your test cases, you must use the following data:

First line
3:12
12:09
4:18 Song with no name
2:22
0:00
To make the marker happy, make that your first test case.

Did you use functions in any of these questions? Should you have? Did you document them correctly?

Does you program "blow up" on unexpected input, or does it deal with bad input in a "graceful" way?

How does your program deal with boundary conditions, if there are any?

Did you remember to put all required comments in? Does your program call out for any other comments in the body of the code?

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Write a c program to calculate the total running time for a
Reference No:- TGS01127219

Now Priced at $60 (50% Discount)

Recommended (97%)

Rated (4.9/5)