How do I change the first letter to uppercase?

I need help with changing information in a file.

This program access the file and changes the all uppercase sentenced to lower case. I used the tolower function.
The trick how do I make the first letter of each sentence uppercase. I know I can use toupper but how do I tell the program where to use it. The sentences will change so they are not fixed in length.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// This program takes uppercase sentences from one file and makes them lowercase
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>  
using namespace std;

int main()
{
    string fileName;         // Holds filename
    char ch;                 // Holds a character
    ifstream inFile;         // Input file
    
    // Open file for output.
    ofstream outFile("challengeOut.txt");
    
    // Get the input file.
    cout << "Enter a file name: ";
    cin >> fileName;
    
    // Open file for input.
    inFile.open(fileName.c_str());
    
    // If input file opened successfully, continue.
    if (inFile)
    {
       // Read char from file 1.
       inFile.get(ch);
       
       // If last read operation was successful.
       while (inFile)
       {
             //Write lowercase char to file 2.
             outFile.put(tolower(ch));
             
             // Read another char from file 1.
             inFile.get(ch);
       }
       
       // Close files.
       inFile.close();
       outFile.close();
       cout << "File conversion done.\n";
    }
    else
        cout << "Cannot open " << fileName << endl;
    system("pause");
    return 0;
}

Last edited on
After every newline character just use toupper instead. You could also read it into a string and just do str[0] = toupper(str[0]);
That's the problem I can't figure out how to tell the computer what to do AFTER the new line. I know I can tell it if (ch = '\n') but I don't know how to tell it to manipulate the next character. I also don't know how to get the very first letter capitalized. I have not worked with strings yet, but it looks like an array. Is it assigned the same way?
Hi @musicman74,
i can figure out now
a way using toupper
and strings (std::string)

but to be honest idk
if its the best;

this is a little example;

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
35
36
37
38
39
//FirstLetter.cpp
//##

#include <iostream>
#include <string>
#include <cstring>

using namespace std;



int main(){

int const NUMBER_OF_NAMES=3;
string names[NUMBER_OF_NAMES];

        names[0]="emily"; 
        names[1]="ariel";
        names[2]="beck";

        //output 3 names
        for(int i=0;i<NUMBER_OF_NAMES;i++)
                cout<<names[i]<<endl;
       

        //changing first letter 
        for(int i=0;i<NUMBER_OF_NAMES;i++)
                names[i].at(0)=toupper(names[i].at(0));
        

        //printing again
        cout<<'\n';
        for(int i=0;i<NUMBER_OF_NAMES;i++)
                cout<<names[i]<<endl;
                


return 0; //indicates success
}//end of main
Eyenrique$ ./FirstLetter 
emily
ariel
beck

Emily
Ariel
Beck
I don't know if that will work or not the sentences are in a file and they have to be changed from uppercase to lowercase with the first letter capitalized. So it's reading one letter at a time. I really just need to know how to signify I only want the first letter of each sentence then I can use toupper.
1
2
3
4
5
6
7
8
9
std::ifstream in("input.txt");
std::ofstream out("output.txt");
std::string line;
while(std::getline(in, line)
{
    line[0] = toupper(line[0]);
    for(std::size_t i = 1; i < line.size(); ++i) line[i] = tolower(line[i]);
    out << line << std::endl;
}


You can iterate different ways or even use std::transform. There are numerous ways to make all but first lower case.

Here is my favorite. std::transform(line.begin() + 1, line.end(), line.begin() + 1, tolower);

http://coliru.stacked-crooked.com/a/40f46e29779eb47e
Thank you. I think I get it. I'll give it a shot and let you know how it works.
Topic archived. No new replies allowed.