Starting from:
$30

$24

Homework 4 Solution

Submission instructions:




You should submit your homework to NYU Classes by the due date. You will be allowed to submit late for one day with a 20% penalty.




Submit one le per question.




Write clean, consistent, and understandable code: use meaningful variable names, one naming convention (either lowercase underscore or camel case), include comments in the beginning and throughout your code, and leave spaces between operators and operands.







Question 1




(From the textbook, page 219, question 4) Ask user to input a string S of odd length:




Write an expression to print the middle character.



Write an expression to print the string up to but not including the middle character (i.e., the rst half of the string).



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.




Write a program using string methods.



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.

1
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 nd the operand strings and the operator symbol, use type con-version 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 numbers.




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 conso-nants 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.







2

More products