FileOperations - Capitalize First Letter of Each Word in Sentence
Apr 30, 2011 at 9:07pm UTC
Hi, I wrote this code first then was asked to modify it so that the program read the contents of the first file and change all the letters to lowercase except the first letter of each sentence, which should be made uppercase. The revised contents should be stored in the second file. How can i form a loop that can do this successfully?
This is the original code:
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
int main()
{
// Variables needed to read and write files.
const int LENGTH = 101;
ifstream inFile; // For input file
ofstream outFile("myOutput.txt" ); // For output file
char myInput[LENGTH], ch, ch2;
string myOutput;
cout << "Lori Bailey" << endl << endl;
// Open input file
cout << "Enter two file names: " ;
cin >> myInput >> myOutput;
inFile.open(myInput);
if (!inFile)
{
cout << "Cannot open " << myInput << endl;
return 0;
}
// Read characters from input file and write uppercased
// versions of the character to the output file.
//myInput[0] = toupper(myInput[0]);
while (inFile.get(ch))
{
ch2 = toupper(ch);
outFile.put(ch2);
}
// Close files.
inFile.close();
outFile.close();
cout << "File conversion done.\n" ;
May 1, 2011 at 6:27am UTC
1 2 3 4 5 6 7 8 9 10 11 12
bool new_sentence = true ;
char ch;
while (in.get(ch)){
if ( ch is a letter ){
if ( new_sentence ) out.put( toupper(ch) );
else out.put( tolower(ch) );
new_sentence = false ;
}else {
if ( ch == '.' ) new_sentence = true ;
out.put( ch )
}
}
Topic archived. No new replies allowed.