Computing.Net > Forums > Programming > to convert uppercase to lowercase

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

to convert uppercase to lowercase

Reply to Message Icon

Name: ahcm
Date: May 30, 2002 at 11:21:12 Pacific
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: gpp
Date: May 30, 2002 at 13:21:13 Pacific
Reply:

I'm very rusty, but I think this is it...

#include

char x='A',y='';
y=tolower(x);

x should now equal... a


0

Response Number 2
Name: Jeff J
Date: May 30, 2002 at 13:23:16 Pacific
Reply:

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


0

Response Number 3
Name: Jeff J
Date: May 30, 2002 at 13:28:51 Pacific
Reply:

..oops, eh heh, that should have been:

char tolower(char chr)
{
if (chr >= 'A' && chr #= 'Z')
chr += 32;
return chr;
}

Sorry about the typo...


0

Response Number 4
Name: ahcm
Date: May 30, 2002 at 13:57:18 Pacific
Reply:

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?


0

Response Number 5
Name: AHCM
Date: May 30, 2002 at 14:17:43 Pacific
Reply:

Help... the following code doesn't work...


#include

char 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);

}


0

Related Posts

See More



Response Number 6
Name: Jim
Date: May 30, 2002 at 15:05:48 Pacific
Reply:

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.


0

Response Number 7
Name: Jeff J
Date: May 30, 2002 at 21:57:05 Pacific
Reply:

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.


0

Response Number 8
Name: second_entity
Date: June 3, 2002 at 22:59:54 Pacific
Reply:

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)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Microsoft Access 2000 Hel... Help with java.



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: to convert uppercase to lowercase

rename files and folder to lowercase with b www.computing.net/answers/programming/rename-files-and-folder-to-lowercase-with-b/19161.html

Convert .asm to .c www.computing.net/answers/programming/convert-asm-to-c/7883.html

c++ code convert .bmp to .JPEG file www.computing.net/answers/programming/c-code-convert-bmp-to-jpeg-file/4383.html