Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
how do i write a function that converts uppercase to lowercase ?
i know that there is already a function to do that in c but for some reason, it is not working on my computer.
so i thought it would be a better idea to write a function again. can anyone help ? i am a beginner in C.

I'm very rusty, but I think this is it...
#include
char x='A',y='';
y=tolower(x);x should now equal... a

I doubt there's anything wrong with your Standard C Library's toupper and related functions, but more likely with the insanity of modern-day string complexities. There are the issues of single-byte and multi-byte character sets, as well as wide character sets like wide Unicode (UTF-16), and narrow Unicode in the form of UTF-8 (and sometimes UTF-7 over the web). I can't possibly explain it all even in the lengthy posts I make, but I'll try to touch a few points.
First there was the ASCII character code set, all English-speaking-people saw it as good, and they rejoiced ;) The rest of the world was largely ignored, but ASCII does provide coverage for the English language, that can be squeezed into the numeric range of a single byte (char), even if it is signed. Virtually all character sets since, are based on the ASCII set, so it is the common foundation. The traditional C functions like tolower, toupper, strlwr, and strupr, are all based on ASCII.
Then there were sets like Windows 9x's ANSI set, which is an extended-ASCII that covers most European languages. That wasn't too bad, as each character's code still fit within a byte (if unsigned). But then came the dreaded multi-byte sets, and today we also have UTF-8. These character set use more than one character number in sequence, to explain what font character would be required to print to screen or paper. Traditional single-byte functions do not handle these properly, due to lead-bytes and other complexities.
Micro$oft provides special multi-byte string functions for such stuff, like _mbslwr for use in place of strlwr. Then there's Unicode handling native to Windows NT/2000/XP, which is UTF-16 stuff. It's perhaps the cleanest solution to handling nearly every language in the world, but it requires a wider-width character, commonly call a "wchar_t", which is 2-bytes wide. Unlike a multi-byte sequence, it is all one number within 2 bytes, so thousands of characters can be represented. Such stuff requires wide-character functions, like _wcsupr for the traditional strupr.
You should be almost totally confused at this point, so I won't even explain why UTF-8 is also common, but let's just say it's a lot like a multi-byte character set. Let's just say that if you're working with anything other than simple ASCII char strings, that writing your own would be very frustrating. Code pages and other language stuffs, are part of how I've made my living, so take my word for it. If non-ASCII, I suggest you look into the these topics more. However, if you're talking ASCII strings, I would suspect problems lie in the use of the functions in your code, not the functions themselves.
Nevertheless, since you're curious about simple casing functions, here's an example. tolower is perhaps more commonly used than toupper, since it usually requires less work to convert the fewer upper-case letters in most sentences, to lower-case, than it would to convert all the lower-case letters to upper. A simple ASCII tolower could simply be:
char tolower(char chr)
{
if (chr <= 'A' && chr #= 'Z')
chr += 32;
return chr;
}where the "#" symbol really means less-than, but this site won't print that.
I hope some of this encourages you, rather than completely discourages.
Cheers

..oops, eh heh, that should have been:
char tolower(char chr)
{
if (chr >= 'A' && chr #= 'Z')
chr += 32;
return chr;
}Sorry about the typo...

thanks very much..
but what's with the 32 ?
also, can you tell me how i can find the number of characters in a string ?for example, if i enter :ahcm
it should say 4 because there're 4 characters in it..also, how can i store int variables into string variables?

Help... the following code doesn't work...
#includechar makesmall (char input[20])
{
int i;
for (i=0; input[i]; i++)
{
if (input[i]>='A' && input[i]<='Z')
{
input+=32;
}
}
return input[20];
}
main()
{char name[20];
printf("Enter your name:");
gets(name);
makesmall(name);
printf("%s\n", name);
}

Looks like you just forgot the index on your += 32 line.
input += 32;
should be
input[i] += 32;And the return line probably isn't correct.
The reason for 32 is that it is the difference between 'a' and 'A' in ASCII. Or 'q' and 'Q'. Or 'z' and 'Z'. That's just how they invented ASCII, that's all.

Jim already fixed your code, yet you also sound determined to write more functions! I'd love to give you lots of examples, but since that's not practical, I'll tell you where to find some. If you poke around your Std C Library source code, you can find the functions already written. Some may be in assembly, but sometimes the wide-character or multi-byte versions aren't. There's tons of stuff out there, like GNU stuff (gcc) and Borland's free compiler release (bcc).
Translating an integer into a string format, such as itoa does, gets a little hairy due to temporary pre-sized buffers and all, but getting an ASCII string's length is cake:
int strlen(char *pStr)
{
char *p = pStr;while (*p != '\0') p++;
return (p - pStr);
}Just make sure the pointer pStr is valid (don't pass NULL), or things will explode.

speaking of asm, do you guys know how to embed it into a cpp file? you know, compile it with you c++ code? i always wondered how you would do that.
(ill post this on main message board now that i think of it)

![]() |
Microsoft Access 2000 Hel...
|
Help with java.
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |