-
May 7th, 2014, 10:12 PM
#1
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
-
May 8th, 2014, 01:04 AM
#2
Re: toupper - tolower
And where in this code snippet do you call these "toupper - tolower"?
Victor Nijegorodov
-
May 8th, 2014, 04:24 AM
#3
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)
-
May 8th, 2014, 11:14 PM
#4
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
-
May 12th, 2014, 11:16 PM
#5
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..
-
May 13th, 2014, 07:16 AM
#6
Re: toupper - tolower
What if there are two sentences on a line? You're changing lines, not sentences.
-
May 13th, 2014, 07:34 AM
#7
Re: toupper - tolower
Originally Posted by GCDEF
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|