Question

Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

Write a C++ 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:
- int main(); o void removeNonAlpha(string& str);
- bool isUpperCaseLetter(char ch);
- bool isLowerCaseLetter(char ch);

The main() function should

1. read string input - an entire line, including spaces (try getline())
2. call removeNonAlpha() to process the input string
3. print out the string as altered by removeNonAlpha()
4. not call isUpperCaseLetter() or isLowerCaseLetter() directly

The function removeNonAlpha() alters its string reference variable by removing all characters that aren't upper or lower case letters or spaces. Use the function erase() in the string class to remove the characters. Use the functions isUpperCaseLetter() and isLowerCaseLetter() to help identify which characters to remove.

The functions isUpperCaseLetter() and isLowerCaseLetter() 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
using namespace std;
bool isUpperCaseLetter(char ch){
   return (ch>='A' && ch<='Z');
}
bool isLowerCaseLetter(char ch){
   return (ch>='a' && ch<='z');
}
// The function removeNonAlpha() alters its string reference variable by removing all characters that aren't upper or lower case letters or spaces.
// Use the function erase() in the string class to remove the characters. Use the functions isUpperCaseLetter() and isLowerCaseLetter() to help
// identify which characters to remove.

void removeNonAlpha(string& str){
   for(int i=0; i<str.length(); i++) {
   if(isUpperCaseLetter(str[i]))
   continue;
   else if(isLowerCaseLetter(str[i]))
   continue;
   else if(str[i] == ' ')
   continue;
   else {
   str.erase(i,1);
   i--;
   }
   }
}

int main(){
// The main() function should
string inString;
// 1. read string input - an entire line, including spaces (try getline())
while(getline(cin, inString)){
// 2. call removeNonAlpha() to process the input string
removeNonAlpha(inString);
// 3. print out the string as altered by removeNonAlpha()
cout <<inString << endl;
// 4. not call isUpperCaseLetter() or isLowerCaseLetter() directly
}
return 0;
}

Add a comment
Answer #2
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdlib> //exit() and EXIT_FAILURE

const char* filename = "C:\\names.txt";

int main()
{
    typedef std::ostream_iterator<std::string> output;
    std::ifstream namefile(filename);
    std::vector<std::string> names;
    std::string input;
    if(!namefile.is_open())
    {
        std::cerr << "Error opening file";
        exit(EXIT_FAILURE);
    }
    while(!namefile.eof())
    {
        namefile >> input;
        names.push_back(input);
    }
    std::cout << "Read " << names.size()
              << " names successfully\n";
    std::copy(names.begin(), names.end(), 
              output(std::cout, "\n"));
    std::cout.flush();
}
Add a comment
Know the answer?
Add Answer to:
Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all th...

    My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • I need help programming this program in C. 1.) Write a program that reads a message,...

    I need help programming this program in C. 1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below: Enter a message: He lived as a devil, eh? Palindrome Enter a message: Madam, I am Adam. Not a Palindrome 2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • Write a C++ console application that reverses an array of characters, and counts the number of:...

    Write a C++ console application that reverses an array of characters, and counts the number of:           Lower case characters (islower)           Upper case characters (isupper)           Digits (isdigit)           Other (not one of previous) Create four global variables to hold these counts. Create function void count(char input[], char reverse[]) that takes as input two arrays: one that contains characters and another that will contain the reverse of that array. The function will reverse the input array and do the...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT