Re: convert a string to all-lowercase string



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
.



Relevant Pages

  • Re: switches
    ... char *cclass; ... int heal, chealth, cmaxhealth; ... then your tolower call expands to: ... scanf ("%s", cname); ...
    (comp.lang.c)
  • Re: reading a C++ structure from a binary file using C#
    ... Encoding instance returned by the static ASCII property on the Encoding ... int roll_no; ... char qualification; ... Sample code would be highly appreciated. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: convert a string to all-lowercase string
    ... Thx for the help:) ... int main{ ... if(isupper((unsigned char) c)) ... I wouldn't have that isuppercall in there; in ASCII at least, tolower ...
    (comp.lang.c)
  • Re: tolower() and toupper()
    ... type, not char? ... int c1, c2; ... since the tolower call will preserve the value EOF. ...
    (comp.lang.c)
  • Re: nitpicking on strcasestr.c
    ... > We have two calls to tolower; the first doesn't cast the result ... the second one casts it to char. ... man tolower ... Noting that tolowerreturns an int, and char's sign is unspecified, ...
    (freebsd-current)