C++ program that filters characters from an input

I am trying to create a program which all input will be provided by a command line argument. The first argument is the name of a text file containing the
input text. There must be an additional argument provided. Your program should read the file named in the first argument as input and generate its output to standard out. The program finishes when the end of the
input file is reached.

Example: cmd: filter input.txt lower

It will remove all lower case letters from the input text file.
Other filters:

Vowel
consonant
word
space
punct
upper or lower

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main( int argc, char *argv[])
{
ofstream myfile("input.txt");
if (myfile.is_open())
{
myfile << "525,600 minutes, how do you measure a year?\n";
myfile.close();
}
else cout << "Unable to open file!\n";
return 0;
}

int main() {
ofstream myfile("input.txt");
if (myfile.is_open())
for (int i=0;text[i]!='\0';i++) {
if (text[i]=='a' || text[i]=='e' || text[i]=='i' || text[i]=='o' || text[i]=='u' || text[i]=='A' || text[i]=='E' || text[i]=='I' || text[i]=='O' || text[i]=='U') {
text[i]=' ';

}






This is what I have s far...
Many of these filters are available in header <cctype> http://en.cppreference.com/w/cpp/header/cctype

The few others (vowel, consonant) would be small functions. Except 'word' which would require special treatment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

int null_filter( int c ) { return c ; }

int is_vowel( int c )
{
    std::string v = "aeiou" ;
    return v.find( std::tolower(c) ) != std::string::npos ;
}

int is_consonant( int c ) { return std::isalpha(c) && !is_vowel(c) ; }

std::ostream& apply_filter( std::ifstream sin, std::ostream& sout, int(*filter_out)(int) )
{
    char c ;
    while( sin.get(c) ) if( !filter_out(c) ) sout.put(c) ;
    return sout ;
}

int main( int argc, char* argv[] )
{
    // validate argc, argv, display usage if invalid etc.
    if( argc != 3 ) return 1 ;
    
    std::string filter_str = argv[2] ;
    for( char& c : filter_str ) c = std::tolower(c) ;

    auto filter = null_filter ;
    
    // TODO: add special case for filter 'word'

    if( filter_str == "upper"  ) filter = std::isupper ;
    else if( filter_str == "lower"  ) filter = std::islower ;
    else if( filter_str == "punct"  ) filter = std::ispunct ;
    else if( filter_str == "vowel"  ) filter = is_vowel ;
    else if( filter_str == "consonant"  ) filter = is_consonant ;
    else if( filter_str == "digit"  ) filter = std::isdigit ;
    // etc ...

    apply_filter( std::ifstream( argv[1] ), std::cout, filter ) ;
}

http://coliru.stacked-crooked.com/a/14363987ace1cf72
Thank you very much, this was definitely a big help!
are you missing 'y' as vowel ?
Topic archived. No new replies allowed.