using toupper

Pages: 12
@kg245407

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void process_text(ifstream& inp_file) 
{
        char n;
        inp_file.get(n);
        
        while(! inp_file.eof())
        {
                if(n=='.') 
                {
                        inp_file.get(n);
                        cout << static_cast<char>(toupper(n));
                }else{
                        cout << n;
                }
                inp_file.get(n);
        }
}


 
cout << static_cast<char>(toupper(n));

//this is correct BUT it only capitalize the letter next to '.'
but for example we have a text file like this.

The quick brown. fox jumps.over the lazy dog. hehe.what??

the output is

The quick brown fox jumpsOver the lazy dog heheWhat??

Now. here's the catch:

The code only capitalizes the letter next to "."
like a.b, the output is aB,
- the period (.) was deleted and the letter next to it was capitalized.
another example
a. b, the output is a b
- since it will capitalize the "space" next to "." not B
Now how should you revise your code??
declare another char data type that will check if the char is a space " "
if the code encounter a "." store it in an array, next step is to check if the
next character next to "." is a space, if it's a space then uppercase the next char next to
space.
or
you can declare a string ". " <-- "<dot><space>"
now use member functions of string class to determine if the text file have a ". "
I'm too lazy today making a code.
hope this helps
Previous comments are right. Original code converts just next symbol after '.' to uppercase, which might be a space not a letter. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void process_text(ifstream& inp_file) 
{
	char n;
	bool up = true;
      
	inp_file.get(n);

	while (!inp_file.eof())	{
		if (n == '.' && up == false) {
			cout << '.';
			up = true;
		} else if (up == true && !isspace(n)) {
			cout << static_cast<char>(toupper(n));
			up = false;		
		} else {
			cout << n;
		}
		inp_file.get(n);
	}
}


I suppose there is more elegant solution using std::string.
Topic archived. No new replies allowed.
Pages: 12