Exercise 5-9
Write a program to write the words in the input that do not contain any uppercase letters followed by words that contain one or more uppercase letters.
Solution
To clarify, the objective of this exercise is to:
- read in a list of words
- print the words that are all pure lower case.
- print the words that contain at least one upper case letter.
To do this, we will need to:
- Use the usual while/cin/push_back method to read in a list of words.
- Create a function hasUpcaseLetters to determine whether a word contains any upper case letters at all.
- If word contains no upper case letters, append to vector<string> allLowcaseLetterWords.
- Otherwise if word contains at least one upper case letters, append to vector<string> someUpcaseLetterWords.
- (Optional) re-use the home-made function vcout to print the two word lists to the console output.
The Project
These are the files that I use. The main program should sum up the logic job flow.
Source File List
Header File List
Source Files
main.cpp
#include <iostream> // cin, cout, endl #include <string> #include <vector> #include "hasUpcaseLetters.h" // hasUpcaseLetters #include "vcout.h" // vcout using std::cin; using std::cout; using std::endl; using std::string; using std::vector; int main() { string word; vector<string> allLowcaseLetterWords; vector<string> someUpcaseLetterWords; // Populate vectors while (cin >> word) { if (hasUpcaseLetters(word)) someUpcaseLetterWords.push_back(word); else allLowcaseLetterWords.push_back(word); } // Print words with at least 1 upper case letter cout << "\nThese words contain at least one upper case letter: " << endl; vcout(someUpcaseLetterWords); // Print pure lower case words cout << "\nThese words are pure lower case letters: " << endl; vcout(allLowcaseLetterWords); return 0; }
hasUpcaseLetters.cpp
#include <cctype> // isupper #include <string> // string using std::isupper; using std::string; bool hasUpcaseLetters(const string& aWord) { for (string::const_iterator i = aWord.begin(); i != aWord.end(); ++i) { if (isupper(*i)) return true; } return false; }
vcout.cpp
#include <iostream> #include <string> // string #include <vector> // vector using std::cout; using std::endl; using std::string; using std::vector; int vcout(const vector<string>& v) { for (vector<string>::const_iterator iter = v.begin(); iter != v.end(); ++iter) { cout << (*iter) << endl; } return 0; }
Header Files
hasUpcaseLetters.h
#ifndef GUARD_HASUPCASELETTERS_H #define GUARD_HASUPCASELETTERS_H #include <string> bool hasUpcaseLetters(const std::string&); #endif // GUARD_HASUPCASELETTERS_H
vcout.h
#ifndef GUARD_HASUPCASELETTERS_H #define GUARD_HASUPCASELETTERS_H #include <string> bool hasUpcaseLetters(const std::string&); #endif // GUARD_HASUPCASELETTERS_H
Test Result
A simple test shows the program works as expected.
Apple apple APPLE aPpLe applE orange oRange ORANGE orangE ^Z These words are pure lower case letters: apple orange These words contain at least one upper case letter: Apple APPLE aPpLe applE oRange ORANGE orangE