CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2014
    Posts
    25

    toupper - tolower

    Hi,

    I need assistance in writing a simple program. It's a simple program really. It reads the contents from one file and change all the letters to lowercase except the first letter in each sentence, which should be uppercase then send to the second file. I'm having difficulties using tolower/toupper. your assistance will be greatly appreciated..

    this is what I have so far...

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <cstdlib>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    
    fstream inputFile, outputFile;
    string text;
    char ch, ch2;
    outputFile.open("output.txt");
    
    
    inputFile.open("input.txt");
    if (!inputFile)
    {
        cout << "File open error!" << endl;
        return 0;
    }
    getline(inputFile,text);
    while(inputFile)
    {
    
        outputFile << text << endl;
        getline(inputFile,text);
    }
    inputFile.close();
    outputFile.close();
    return 0;
    
    }
    Thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: toupper - tolower

    And where in this code snippet do you call these "toupper - tolower"?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: toupper - tolower

    First you need to think about the program design and how you are going to achieve your requirements. You want the first letter of each sentence to be uppercase and the rest lowercase. Making a letter uppercase or lowercase is easy using the toupper() or tolower() functions for a single character. The issue you have is deciding which is the first letter of a sentence. What constitues the start of a sentence? Once you have decided logically when a sentance starts, then you need to parse the obtained string lines to determine the first letter of the sentence. Only once when you know which character this is can you then convert it to uppercase. Conversly, if the character is not required to be uppercase then it is required to be lowercase. Note that toupper() and tolower() have no effect on non-letters.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Apr 2014
    Posts
    25

    Re: toupper - tolower

    ok thanks I think I have a better understanding on how to do it. I will work on it this weekend thank

  5. #5
    Join Date
    Apr 2014
    Posts
    25

    Re: toupper - tolower

    Sorry I took so long to get back, I was able to figure it out after taking your advise and doing a little research..

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <cstdlib>
    #include <fstream>
    using namespace std;
    
    ifstream infile;
    ofstream outfile;
    void lower(string &);
    int main()
    {
        string infileName,outfileName,line;
        cout<<"Enter infile name ";
        cin>>infileName;
        cout<<"Enter outfile name ";
        cin>>outfileName;
    
        infile.open(infileName.c_str());
        outfile.open(outfileName.c_str());
    
        if(!infile.is_open()||!outfile.is_open()){
            cout<<"Files could not be opened";
            return -1;
        }
    
        infile.clear();
        while(!infile.eof())
        {
            getline(infile,line);
            lower(line);
            outfile<<line<<endl;
        }
    
        infile.close();
        outfile.close();
        return 0;
    }
    
    void lower(string &line)
    {
        line[0]=toupper(line[0]);
        for(int i=1; i<line.length(); i++){
            line[i]=tolower(line[i]);
        }
        return;
    }
    thanks..

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: toupper - tolower

    What if there are two sentences on a line? You're changing lines, not sentences.

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: toupper - tolower

    Quote Originally Posted by GCDEF View Post
    What if there are two sentences on a line? You're changing lines, not sentences.
    Or one sentence over more than one line?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured