Upper Case to Lower Case

Hello guys, i am a beginner at programing, and I found this site really helpful as it has c++ tutorials, all the libraries and its functions with example code, and the forums. I been trying to change upper case letters to lower case, first i did it with type casting and the ASCII code, but I found that it was possible with the library ctype, but I don't understand any of the example codes, are they in C? I program in bloodshed dev-C++.

Program with type casting:

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 <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char A[10];
    int largo, sum=0;
    for (int a=1; A[a-1]!='\n'; a++){
        cin.get(A[a]);
        largo=a;
        if (int (A[a])>=65 and int (A[a])<=90){
                A[a]=int (A[a])+32;
                sum+=int (A[a]);
        }
    }
    cout<<endl;
    cout<<"Sum: "<<sum<<endl;
    string trash;
    getline(cin, trash); 
    getline(cin, trash);
    return EXIT_SUCCESS;
}


Program with the library Cstring:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])
{
    char A[20];
    cin>>A;
    cout<<endl;
    for (int b=0; b<=strlen(A); b++){
        tolower(A[b]);
        cout<<A[b];
    }
    cout<<endl;
    string trash;
    getline(cin, trash); 
    getline(cin, trash);
    return EXIT_SUCCESS;
}


My questions are: what do u recomend me to understand the examples that are around the libraries explanation? make the tutorials? Because i program differently from the examples; and how can I change upper to lower case letters using the ctype library, using tolower?
Last edited on
Not sure what's ur doubt.
You already have what you want:

Just include this librabry <cctype>
and when you want to change the case of any character,simply use tolower and toupper function and pass that character as an argument.
Yes, the examples are in C++

And i recommend you to read through the whole tutorial on this site, it has lots of examples also
My doubt is that in the second code, the tolower doesn't work and i have the cctype library; any idea why?
Last edited on
std::tolower doesn't write into the variable, it just returns the lower case character value for the character you pass to it.
Your loop goes too far and you didn't assign the modified letter back to the char array:
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
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])
{
    char A[20];
    cin>>A;
    cout<<endl;
//    for (int b=0; b<=strlen(A); b++) // loops too far
    for (int b=0; b < strlen(A); b++) // b < strlen(A) is correct
    {
//    	tolower(A[b]); // this doesn't change the value
    	A[b] = tolower(A[b]); // need to assign the output of tolower()
        cout<<A[b];
    }
    cout<<endl;
    string trash;
    getline(cin, trash);
    getline(cin, trash);
    return EXIT_SUCCESS;
}

Also you might want to look at the std C++ libraries for this too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
	std::string str = "Just Some Text";

	std::transform(str.begin(), str.end(), str.begin(), std::ptr_fun(tolower));
	std::cout << str << std::endl;

	std::transform(str.begin(), str.end(), str.begin(), std::ptr_fun(toupper));
	std::cout << str << std::endl;
}


Last edited on
Topic archived. No new replies allowed.