COMP B12 Assignment #1 C++ INTRODUCTION The purpose of this lab is to give you a
ID: 3879088 • Letter: C
Question
COMP B12 Assignment #1 C++ INTRODUCTION The purpose of this lab is to give you an opportunity to practice programming in C++ and to familiarize yourself with string I/O and string manipulation. Filter Write a program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following Place your code in a file called filterChars.cpp The program should consist of four functions whose prototypes are o int main(); o void removeNonAlpha (string& str); o bool isUpperCaseLetter (char ch); o bool isLowerCaseLetter(char ch); . The mainO function should 1. read string input an entire line, including spaces (try getline0) 2. call removeNonAlpha0 to process the input string 3. print out the string as altered by removeNonAlpha0 4. not call isUpperCaseLetterO or isLowerCaseLetter) directly The function removeNonAlpha0 alters its string reference variable by removing all characters that aren't upper or lower case letters or spaces. Use the function eraseO in the string class to remove the characters. Use the functions isUpperCaseLetterO and isLowerCaseLetterO to help identify which characters to remove The functions isUpperCaseLetter) and isLowerCaseLetterO simply return true if the character parameter is an upper-case or lower-case letter, respectively, and false otherwise. These two functions can easily be one-liners. Note: Don't use any library functions (specifically, use neither islower nor isupper) in isUpperCaseLetter0 and isLowerCaseLetter). Use an if statement or a Boolearn expression instead. Why? For practice. Getting the input to work correctly can be kind of tricky. Try using while(getline(cin, inString)) Shown below is a sample run of the program.Explanation / Answer
C++ Code:
/***
* Author:
* course Name:
* date:
* source file name: filterChars.cpp
***/
#include <iostream>
#include<string>
using namespace std;
//This function returns true if ch is uppercase letter and false otherwise
bool isUpperCaseLetter(char ch)
{
return (ch>='A'&&ch<='Z')?true:false;
}
//This function returns true if ch is lowercase letter and false otherwise
bool isLowerCaseLetter(char ch)
{
return (ch>='a'&&ch<='z')?true:false;
}
//This function removes all chars other than upper, lowercase letters and spaces
void removeNonAlpha(string& str)
{
//for each character
for(int i=0;i<str.length();i++)
{
//if not upper or lower case or space then erase it
if(!(isUpperCaseLetter(str[i])||isLowerCaseLetter(str[i])||str[i]==' '))
{
str.erase(i,1);
}
}
}
int main() {
string inString; //to store input string
//read lines
while(getline(cin,inString))
{
removeNonAlpha(inString); //call function to filter string
cout<<inString<<endl; //print filterd string
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.