Assignment 14
Due Monday, March 5 at 9:20am on Canvas
Don't forget to do Activity 14.2 for homework!
Assignment 14.1: Lower and Upper Case (10 pts)
- Let's write a program to do the following:
- convert a string to all lower-case letters
- convert a string to all upper-case letters
- To approach this problem, we need to remember the ASCII table and how it works.
- Recall that upper case letters have ASCII values between 65 and 90.
- Lower case letters have ASCII values between 97 and 122.
- Verify that these facts are correct by looking at the ASCII Table.
- Finally, note that lower case 'a' is the integer number 97 and upper case 'A' is the integer number 65.
- Therefore, there is a difference of 32 between the ASCII value of any lower case character and its upper case version.
- We will use these facts to our advantage in this program.
- Create a new file called upperLower.cpp.
- Copy and paste the starter code below into your file:
/*
*name
*section
*/
#include <iostream>
using namespace std;
int main () {
string sentence;
return 0;
}
- Add some code to welcome the user, prompt the user to enter a sentence and then store the sentence as a string variable.
- Verify that your program compiles and runs.
- Now, let's add some statements.
- We will need to use a for loop, string indexing and our knowledge of ASCII to complete this task.
- Write a for loop to cycle through our string
variable that was passed in as a parameter to our function.
for (int i = 0; i < sentence.length(); i++) {
//statements for string processing go in here
}
- Inside
the loop, we need to find out if this is a capital letter or not. We will use an
if statement to test whether the ASCII value lies between 65 and 90.
if (sentence[i] >= 65 && sentence[i] <= 90) {//if this character is between 65 and 90, it is a capital
}
- What should go inside the if statement?
- Consider
that each upper case character has an ASCII value that is 32 less than
each lower case character.
- So, if it is an uppercase letter, we will
need to add 32 to its ASCII value and then convert it back to a char.
- Add the below statements inside the curly braces of the if.
sentence[i] += 32;
//adding 32 to sentence[i]'s ASCII value
- Now, run your program and verify that it works correctly by looking at the output of the test in main.
- For the rest of the program, your job is to write the remaining for loop to convert the sentence to all upper case letters.
- When you are finished, submit your work to Canvas.
Your output should look identical to the output below:
Please enter a sentence: I love cookies!
Your sentence in all upper case: I LOVE COOKIES!
Your sentence in all lower case: i love cookies!
Assignment 14.2: Valid Email Address? (10 pts)
- Write a program that takes in an email address as a string and determines whether the email address is valid.
- For the purposes of this assignment, we will use two criteria to determine if an email address is valid:
- The address must end with one of the following extensions: .com, .org or .edu
- The address must have an @ character contained somewhere in the string
- The address must not contain a space.
- Hint: Use a for loop combined with string indexing to test for the @ symbol and the space.
- Copy and paste the starter code into a new file called email.cpp
#include <iostream>
using namespace std;
int main() {
string email;
bool containsAt = false;
bool containsSpace = false;
bool validEmail = true;
cout << "Please enter an email address: ";
//read in the email address to the email variable
//for loop to determine if email address contains a space
for (int i = 0; i < email.length(); i++) {
if (email[i] == ' ') {
containsSpace = true;
}
}
//Write a similar for loop to check for the at symbol
if (containsSpace == true) {
cout << "Your email address cannot contain a space." << endl;
validEmail = false;
}
if (containsAt == false) {
cout << "Your email address must contain an @ symbol." << endl;
validEmail = false;
}
//Add if statement to check if email has correct extension
if (validEmail == true) {
cout << "Your email address is valid." << endl;
} else {
cout << "Your email address is not valid." << endl;
}
return 0;
}
- Fill in the missing parts of the program indicated by the comments.
- Please do not make any other changes.
- Your program must work identically to the sample output below.
- When your program is giving the correct output, upload your source code to Canvas.
Please enter an email address: bob@jobs.net
Your email address must end with .com, .edu or .org
Your email address is not valid.
Alternately,
Please enter an email address: bobajobs.net
Your email address must contain an @ symbol.
Your email address must end with .com, .edu or .org
Your email address is not valid.
Alternately,
Please enter an email address: bob a jobs.co
Your email address cannot contain a space.
Your email address must contain an @ symbol.
Your email address must end with .com, .edu or .org
Your email address is not valid.
Alternately,
Please enter an email address: boba jobs.com
Your email address cannot contain a space.
Your email address must contain an @ symbol.
Your email address is not valid.
Alternately,
Please enter an email address: bob@jobs.com
Your email address is valid.
|
|