CS 1114 – Homework 4 solved


Category: You will receive a download link of the .ZIP file upon Payment


5/5 - (1 vote)

Question 1
(From the textbook, page 219, question 4) Ask user to input a string S of odd length:
(a) Write an expression to print the middle character.
(b) Write an expression to print the string up to but not including the middle character (i.e., the
first half of the string).
(c) Write an expression to print the string from the middle character to the end (not including the
middle character).
Sample output:
Enter a string: Fortune favors the bold
Middle character: o
First half: Fortune fav
Second half: rs the bold
Question 2
Ask user to input a character and classify it to one of the following: lower case letter, upper case
letter, digit, or non-alphanumeric character. Note: a character is a length 1 string.
(a) Write a program using string methods.
(b) Write a second program without string methods.
Sample output (multiple executions):
Enter a character: j
j is a lower case letter.
Enter a character: 7
7 is a digit.
Enter a character: ^
^ is a non-alphanumeric character.
Enter a character: C
C is an upper case letter.
Question 3
Write a program that will read and evaluate a mathematical expression of the form oprand1 op
operand2, where operand1 and operand2 are positive integers and op is an operator (that is, + –
* or /, for example: 24 + 65 or 276 * 2). Assume that there is a space between each operand and
operator. Note: You only need to consider the following operators: + – * /.
Hint: Use string operations to find the operand strings and the operator symbol, use type conversion functions to convert the operands to numbers, and use some kind of ”if” statement(s) to
check which operator symbol was given and apply the corresponding operator. For example if the
operator symbol is the ’+’ character, the program should recognize that and add the two operand
Sample output (multiple executions):
Enter a mathematical expression: 5 + 10
5 + 10 = 15
Enter a mathematical expression: 81 / 9
81 / 9 = 9
Question 4
Write a program that reads in an English word and prints how many vowels and how many consonants it contains. You may assume the letter Y is not a vowel.
Sample output (multiple executions):
Enter a word: Gallagher
Gallagher has 3 vowels and 6 consonants.
Enter a word: Sterling
Sterling has 2 vowels and 6 consonants.
Question 5
The ACME Widgets company has a new password policy. Passwords must be at least 8 characters
long and must contain the following:
• At least two uppercase letters
• At least one lowercase letter
• At least two digits
• At least one special character: ! @ # $
Write a program that reads in a string and determines whether or not it is a valid password.
Sample output (multiple executions):
Enter a password: P4Ssword1!
P4Ssword1! is a valid password.
Enter a password: password
password is not a valid password.