How do I convert text in upper case to lower case? :?:

This function converts a string to lowercase usering OR

char *ToLowerCase(char *text) {
	char *str = new char[strlen(text)];
	register int i;
	for (i=0;text[i];i++)
		str[i] = text[i] >= 'A' && text[i] <= 'Z' ?
		text[i] | 32 : text[i];
	str[i] = '\0';
	return str;
}

Well thats pretty easy.

Use the library

cctype

#include <cctype>

And then you can use the tolower command. It just is opposite of toupper.

char c;
cin>>c;
char d=tolower(c);
cout<<d;

Then that code takes in a character and converts into lowercase.

commented: Excellent!! +1

It works because each character contains a value, for example:

'A' is equal 65 // 01100001

01000001 // 65
OR    00100000 // 32
     ----------
      01100001 // 97

97 = 'a'

> It works because each character contains a value
It works because you assume the the character set is contiguous, and is basically US-ASCII
Read about http://en.wikipedia.org/wiki/EBCDIC
Then read about locales.

Using toupper() etc will hide the locale and encoding details from you.


> char *str = new char[strlen(text)];
You also forgot to allocate space for the \0 at the end.

Thanks, that worked perfectly!! :)

> It works because you assume the the character set is contiguous, and is basically US-ASCII
Or Unicode, and Unicode is the future of character sets. Edward definitely recommends toupper, but when was the last time you worked on an IBM mainframe? ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.