简体   繁体   中英

How to convert mixed letters and numbers to letters letters only? C++

I'm trying to get a grasp on how to do this assignment but my experience is limited. The idea is that I have to call a text file that contains letters (lower and upper case) and numbers all mixed, no spaces, and convert it to lower case only.

I would appreciate help with the first part which is taking out any numbers from the text and leaving only letters.

Pseudocode only, since OP didn't post an attempt:

string data = ReadFileData();
for (char character : data)
{
    if (islower(character)) print (character);    // Or output to file, append to string, etc.
}

It can be done in several ways. I am only giving you the full code so that you can learn. Try other approaches on your own.

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



int main()
{
    ifstream fin("input.txt");
    ofstream fout("output.txt");

    if(!fin)
    {
        cout << "Error opening file!";
        return -1;
    }

    char c;

    while(fin.get(c))
    {
        if((c >= 'A' && c <= 'Z' ) || (c >= 'a' && c <= 'z' ))
        {
            fout << c;
        }        
    }

    fout.close();
    fin.close();

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM