Upper case letters

how can I write a program that converts upper case letters to lower case letters?

All characters are integers and one way to see their values is to goole "ascii table":
http://www.asciitable.com/index/asciifull.gif

In code, you can do:
1
2
char ch = 'a';
std::cout << (int)ch << '\n';


So the trick is to do the correct math to switch upper to lower.
Last edited on
Character set is implementaton defined (standard 3.9.1.1) and such math is non-portable.
there is tolower() function in <cctype> header
closed account (3qX21hU5)
I would use tolower() and toupper() like MiiNiPaa suggested. Here is a example, you iterator through the string and convert each character to uppercase or lowercase character by character. You can do this by using a iterator or using indexes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main ()
{
    string words ("Hello how are you");

    cout << "Normal: " << words << endl;

    for (string::iterator it = words.begin(); it != words.end(); ++it)
       *it = toupper(*it);

    cout << "All Upper case: " << words << endl;

    for (string::size_type i = 0; i != words.size(); ++i)
        words[i] = tolower(words[i]);

    cout << "All lower case: " << words << endl;

    return 0;
}
Last edited on
u can create a function

1
2
3
4
5
void stringtoUpper( string &s ){
    for( int i = 0 ; i < s.length() ; i++ ){
        s[i] = toupper(s[i]);
    }
}

it's more efficient
and include this
 
#include <ctype.h> 

You can do this by using a iterator
Why not range-based loops?
1
2
3
for(char& x: words) {
    x = tolower(x);
}


and include this
#include <ctype.h>
<cctype>, .h versions of headers are deprecated.
closed account (3qX21hU5)
I was thinking about showing ranged based loops in the example but didn't want to complicate it to much for the OP since he is still learning (I believe though I might be wrong), and because of the possibility that he is using a non C++11 compiler.
Character set is implementaton defined (standard 3.9.1.1) and such math is non-portable.


If your c/c++ compiler isnt using the ASCII character set, you've got issues.

Also:
std::transform(str.begin(), str.end(), str.begin(), toupper);

But that sort of takes the whole learning aspect out of it.
Last edited on
You can also do this
1
2
    string words = "HeLLo WOrLd";
    for(unsigned int i = 0; i<words.size(); i++) words[i] = tolower(words[i]);


EDIT: forget what I said I didn't realize Lim Boon Jye said pretty much same exact thing
Last edited on
If your c/c++ compiler isnt using the ASCII character set, you've got issues.
Welcome to the world of pain locale codepages: http://en.wikipedia.org/wiki/Code_page_866 or "standard" http://en.wikipedia.org/wiki/KOI8-R — look at the letter 'Ё' (B3) and switched order of lowercase-uppercase letters in english and russian.
Also in local train station display controller uses codepage like "'\0' 1234567890AaBbCc/*...*/АаБбВв/*...punctuation and other*/"
Topic archived. No new replies allowed.