Re: convert a string to all-lowercase string
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 03 Jul 2006 16:28:57 -0400
Andrew Poelstra wrote:
On 2006-07-03, santosh <santosh.k83@xxxxxxxxx> wrote:
federico_bert...@xxxxxxxxxx wrote:
err... hehe,
I understand what do you mean but if you can post a sample code please!
(I'm a newbye)
Thx for the help :)
#include <stdio.h>
#include <ctype.h>
int main(void) {
int c;
while((c = getchar()) != EOF) {
if(isalpha((unsigned char) c)) {
if(isupper((unsigned char) c))
c = tolower((unsigned char) c);
}
putchar(c);
}
return 0;
}
I wouldn't have that isupper() call in there;
Nor would I; it is unnecessary.
in ASCII at least, tolower
can be implemented as a single AND,
You probably mean either that toupper() can be implemented
as a single AND, or that tolower() can be implemented as a
single OR. Either way, you're wrong: consider tolower('^') as
a counter-example.
and therefore would be more efficient
(and perhaps more clear) if you simply tested for an alpha and then did
tolower.
Even the isalpha() test is unnecessary.
Long ago in the Bad Old Days the world was beset by Evil
Implementations of toupper() and tolower() that only worked
correctly on lower- or upper-case arguments, things like
#define toupper(c) ((c) + 'A' - 'a') /* BADDD! */
#define tolower(c) ((c) + 'a' - 'A') /* WRONG! */
Code like this never really worked at all: it had a nasty way
of misbehaving on things like toupper((unsigned char)'ä'), for
example (that's a lower-case A with diaresis, if you're having
trouble seeing it). This kind of implementation became non-
conforming the very instant there was a Standard to conform to,
almost seventeen years ago.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- References:
- convert a string to all-lowercase string
- From: federico_bertola
- Re: convert a string to all-lowercase string
- From: santosh
- Re: convert a string to all-lowercase string
- From: federico_bertola
- Re: convert a string to all-lowercase string
- From: santosh
- Re: convert a string to all-lowercase string
- From: Andrew Poelstra
- convert a string to all-lowercase string
- Prev by Date: Re: So what Standard are we working off?
- Next by Date: Re: What do you think about the code?
- Previous by thread: Re: convert a string to all-lowercase string
- Next by thread: Re: convert a string to all-lowercase string
- Index(es):
Relevant Pages
|