toupper - tolower
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: toupper - tolower

  1. #1
    Join Date
    Apr 2014
    Posts
    14

    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
    Wallisellen (ZH), Switzerland
    Posts
    17,606

    Re: toupper - tolower

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

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,848

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Apr 2014
    Posts
    14

    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
    14

    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,228

    Re: toupper - tolower

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

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,848

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center