Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

[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?

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M91416814
  • Price:- $60

Priced at Now at $60, Verified Solution

Have any Question?


Related Questions in C/C++

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As