By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

convert a string to all-lowercase string

P: n/a
Hi everybody,
I have an array of chars that I want to make all lower

int Scan(char Search[])
{
char *cPtr;
cPtr = strtok (Search," -,.");

while (cPtr != NULL)
{
// Here I want to lower cPtr whith somithing like cPtr =
tolower(cPtr)
//but don't works
printf("%s ", cPtr);
cPtr = strtok (NULL, " -,.");
}
}

Help me please!
Thx a lot! :)

Jul 3 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a
fe**************@tiscali.it wrote:
Hi everybody,
I have an array of chars that I want to make all lower

int Scan(char Search[])
{
char *cPtr;
cPtr = strtok (Search," -,.");

while (cPtr != NULL)
{
// Here I want to lower cPtr whith somithing like cPtr =
tolower(cPtr)
//but don't works
printf("%s ", cPtr);
cPtr = strtok (NULL, " -,.");
}
}
strtok() is a subtly complicated function to use. Just loop through
your array testing for an alphabetic character with isalpha() and if
so, check if it's already in lowercase with islower() and if not,
convert with tolower().

Jul 3 '06 #2

P: n/a
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 :)

Jul 3 '06 #3

P: n/a
federico_bert...@tiscali.it 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;
}

Jul 3 '06 #4

P: n/a

santosh ha scritto:
federico_bert...@tiscali.it 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;
}
Thx again :)))

Jul 3 '06 #5

P: n/a
fe**************@tiscali.it wrote:
santosh ha scritto:
federico_bert...@tiscali.it 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 followed Richard Heathfield in the matter of casting the arguments to
isXXX() and toXXX() functions to unsigned char. Please see the
following post where an excellent explanation of the reason for the
cast is given by Richard:

<http://groups.google.com/group/comp.lang.c/msg/84a6b5f62513a0f7?hl=en&>

Jul 3 '06 #6

P: n/a
On 3 Jul 2006 09:39:28 -0700, "santosh" <sa*********@gmail.comwrote
in comp.lang.c:
fe**************@tiscali.it wrote:
Hi everybody,
I have an array of chars that I want to make all lower

int Scan(char Search[])
{
char *cPtr;
cPtr = strtok (Search," -,.");

while (cPtr != NULL)
{
// Here I want to lower cPtr whith somithing like cPtr =
tolower(cPtr)
//but don't works
printf("%s ", cPtr);
cPtr = strtok (NULL, " -,.");
}
}

strtok() is a subtly complicated function to use. Just loop through
your array testing for an alphabetic character with isalpha() and if
so, check if it's already in lowercase with islower() and if not,
convert with tolower().
The use of isalpha() and islower() functions is completely unnecessary
in this case.

islower() alone only returns non-zero if its argument is a lower case
alpha character.

But neither function is required. tolower(), for a valid argument,
returns a different value from the argument if and only if the
argument is an upper case alpha character with a lower case
equivalent, otherwise it returns its argument unchanged.

Note that the argument must be valid, however, namely an int with a
value of either EOF or in the range of 0 to UCHAR_MAX inclusive. On
implementations where char is signed, some characters might have
negative values and passing one of those to any of the to... or is...
functions produces undefined behavior.

The usual advice given here is to cast each character to unsigned char
when passing to the to... or is... functions. I don't particularly
like that approach, because some characters with negative values (such
as PC text mode "extended ASCII") will get changed that you probably
don't want changed.

The safest way to do this to only valid uppercase letters is something
like this (uncompiled and untested):

#include <ctype.h>

char *(string_to_lower char *s)
{
char *ret = s;

if (s)
{
while (*s)
{
if (*s 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 3 '06 #7

P: n/a

santosh ha scritto:
federico_bert...@tiscali.it 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;
}
Thx again :)))

Jul 3 '06 #8

P: n/a
Jack Klein wrote:
On 3 Jul 2006 09:39:28 -0700, "santosh" <sa*********@gmail.comwrote
in comp.lang.c:
fe**************@tiscali.it wrote:
Hi everybody,
I have an array of chars that I want to make all lower
>
int Scan(char Search[])
{
char *cPtr;
cPtr = strtok (Search," -,.");
>
while (cPtr != NULL)
{
// Here I want to lower cPtr whith somithing like cPtr =
tolower(cPtr)
//but don't works
printf("%s ", cPtr);
cPtr = strtok (NULL, " -,.");
}
}
strtok() is a subtly complicated function to use. Just loop through
your array testing for an alphabetic character with isalpha() and if
so, check if it's already in lowercase with islower() and if not,
convert with tolower().

The use of isalpha() and islower() functions is completely unnecessary
in this case.

islower() alone only returns non-zero if its argument is a lower case
alpha character.

But neither function is required. tolower(), for a valid argument,
returns a different value from the argument if and only if the
argument is an upper case alpha character with a lower case
equivalent, otherwise it returns its argument unchanged.
Points noted. Thanks.
Note that the argument must be valid, however, namely an int with a
value of either EOF or in the range of 0 to UCHAR_MAX inclusive. On
implementations where char is signed, some characters might have
negative values and passing one of those to any of the to... or is...
functions produces undefined behavior.

The usual advice given here is to cast each character to unsigned char
when passing to the to... or is... functions. I don't particularly
like that approach, because some characters with negative values (such
as PC text mode "extended ASCII") will get changed that you probably
don't want changed.

The safest way to do this to only valid uppercase letters is something
like this (uncompiled and untested):

#include <ctype.h>

char *(string_to_lower char *s)
{
char *ret = s;

if (s)
{
while (*s)
{
if (*s 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}
Thanks for the code.

PS: You probably meant to write:
char *string_to_lower(char *s)

Jul 3 '06 #9

P: n/a
On 2006-07-03, santosh <sa*********@gmail.comwrote:
federico_bert...@tiscali.it 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; in ASCII at least, tolower
can be implemented as a single AND, and therefore would be more efficient
(and perhaps more clear) if you simply tested for an alpha and then did
tolower.

--
Andrew Poelstra <http://www.wpsoftware.net/blog>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 3 '06 #10

P: n/a
santosh wrote:
federico_bert...@tiscali.it 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);
I would simplify the above three lines by deleting the tests. For
non-alpha and for lower case letters tolower will return its input.
}
putchar(c);
while((c = getchar()) != EOF)
putchar(tolower(c));

getchar returns the character already in the range of unsigned char, so
in this case you don't even need the cast. Much simpler.
}
return 0;
}
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 3 '06 #11

P: n/a
Andrew Poelstra wrote:
On 2006-07-03, santosh <sa*********@gmail.comwrote:
federico_bert...@tiscali.it 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; in ASCII at least,
You mean under ISO C?
tolower can be implemented as a single AND, and therefore would be more efficient
(and perhaps more clear) if you simply tested for an alpha and then did tolower.
Yes, I should've done that. As an aside, Jack Klien has eliminated even
isalpha() in his method, which he's posted elsethread.

Jul 3 '06 #12

P: n/a
posted:
Hi everybody,
I have an array of chars that I want to make all lower

How about:

#include <ctype.h>

inline void StringToLower(char *p)
{
while ( *p = tolower(*p) ) ++p;
}
From what I've read, it seems that "tolower" returns the original value if
it was unable to convert it to lowercase, so we should have no worries
about passing it numbers, lowercase letters, symbols, or a null character.
If I were to change the loop slightly, would it violate the "sequence
point" rule?

while( *p++ = tolower(*p) );
--

Frederick Gotham
Jul 3 '06 #13

P: n/a
On 2006-07-03, Jack Klein <ja*******@spamcop.netwrote:
>
#include <ctype.h>

char *(string_to_lower char *s)
{
char *ret = s;

if (s)
{
while (*s)
{
if (*s 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}
This is more compact (and doesn't start with "str", invading implementation
namespace):

char *lowercase_string (char *s)
{
if (s)
while (*s)
{
*s = (*s 0) ? tolower (*s) : *s;
s++;
}
}

--
Andrew Poelstra <http://www.wpsoftware.net/blog>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Jul 3 '06 #14

P: n/a
On 3 Jul 2006 10:35:18 -0700, "santosh" <sa*********@gmail.comwrote
in comp.lang.c:
Jack Klein wrote:
On 3 Jul 2006 09:39:28 -0700, "santosh" <sa*********@gmail.comwrote
in comp.lang.c:
fe**************@tiscali.it wrote:
Hi everybody,
I have an array of chars that I want to make all lower

int Scan(char Search[])
{
char *cPtr;
cPtr = strtok (Search," -,.");

while (cPtr != NULL)
{
// Here I want to lower cPtr whith somithing like cPtr =
tolower(cPtr)
//but don't works
printf("%s ", cPtr);
cPtr = strtok (NULL, " -,.");
}
}
>
strtok() is a subtly complicated function to use. Just loop through
your array testing for an alphabetic character with isalpha() and if
so, check if it's already in lowercase with islower() and if not,
convert with tolower().
The use of isalpha() and islower() functions is completely unnecessary
in this case.

islower() alone only returns non-zero if its argument is a lower case
alpha character.

But neither function is required. tolower(), for a valid argument,
returns a different value from the argument if and only if the
argument is an upper case alpha character with a lower case
equivalent, otherwise it returns its argument unchanged.

Points noted. Thanks.
Note that the argument must be valid, however, namely an int with a
value of either EOF or in the range of 0 to UCHAR_MAX inclusive. On
implementations where char is signed, some characters might have
negative values and passing one of those to any of the to... or is...
functions produces undefined behavior.

The usual advice given here is to cast each character to unsigned char
when passing to the to... or is... functions. I don't particularly
like that approach, because some characters with negative values (such
as PC text mode "extended ASCII") will get changed that you probably
don't want changed.

The safest way to do this to only valid uppercase letters is something
like this (uncompiled and untested):

#include <ctype.h>

char *(string_to_lower char *s)
{
char *ret = s;

if (s)
{
while (*s)
{
if (*s 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}

Thanks for the code.

PS: You probably meant to write:
char *string_to_lower(char *s)
Yes, I probably did. Thanks.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 3 '06 #15

P: n/a
On Mon, 03 Jul 2006 18:54:57 GMT, Andrew Poelstra
<ap*******@localhost.localdomainwrote in comp.lang.c:
On 2006-07-03, Jack Klein <ja*******@spamcop.netwrote:

#include <ctype.h>

char *(string_to_lower char *s)
{
char *ret = s;

if (s)
{
while (*s)
{
if (*s 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}

This is more compact (and doesn't start with "str", invading implementation
namespace):
Quibble: names beginning with "str", "is", "to", and (maybe, can't
remember, can't bother to look it up) "mem" are reserved at file scope
only when followed by another lower case letter.

Of course, I goofed, should have been str_to_lower.

And as santosh pointed out, I got the function definition garbled.

Happy Monday.
char *lowercase_string (char *s)
{
if (s)
while (*s)
{
*s = (*s 0) ? tolower (*s) : *s;
s++;
}
}
And returning what? Must be Monday where you are, too!

I probably wouldn't write it that way myself, but I certainly wouldn't
in a follow-up to a thread from an obvious newbie. YMMV.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 3 '06 #16

P: n/a
Andrew Poelstra wrote:
On 2006-07-03, santosh <sa*********@gmail.comwrote:
>>federico_bert...@tiscali.it 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
es*****@acm-dot-org.invalid
Jul 3 '06 #17

P: n/a
Jack Klein wrote:
>
The usual advice given here is to cast each character to unsigned char
when passing to the to... or is... functions. I don't particularly
like that approach, because some characters with negative values (such
as PC text mode "extended ASCII") will get changed that you probably
don't want changed.
If you'd rather have '' remain unchanged instead of
being replaced by '', that's your choice -- but leaving
it unchanged doesn't agree with the Standard's notion of
"lower case."
The safest way to do this to only valid uppercase letters is something
like this (uncompiled and untested):

#include <ctype.h>

char *(string_to_lower char *s)
{
char *ret = s;

if (s)
{
while (*s)
{
if (*s 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}
I cannot see any reason to call this "safer" than the
recommended alternative: Lose the test and add the cast.
That's the recipe that puts the argument in the form that
tolower() and all the other <ctype.hfunctions expect. If
you like to call the library functions in screwball ways, it
is not their fault if you get unsatisfactory results.

--
Eric Sosman
es*****@acm-dot-org.invalid

Jul 3 '06 #18

P: n/a
"santosh" <sa*********@gmail.comwrites:
federico_bert...@tiscali.it 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;
}
Note that this doesn't address the original question. The OP wanted
to convert a string to lower case; you're converting stdin to lower
case and writing it to stdout. (Perhaps this was intentional, to
avoid doing the OP's homework.)

Others have pointed out the redundancy of the isalpha() and isupper()
calls.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 3 '06 #19

P: n/a
Keith Thompson wrote:
"santosh" <sa*********@gmail.comwrites:
federico_bert...@tiscali.it 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;
}

Note that this doesn't address the original question. The OP wanted
to convert a string to lower case; you're converting stdin to lower
case and writing it to stdout. (Perhaps this was intentional, to
avoid doing the OP's homework.)
Indeed, though in hindsight my post has, (justifiably), recieved so
many replies, I wonder if the OP is confused, (he admitted to being a
newbie).
Others have pointed out the redundancy of the isalpha() and isupper()
calls.
I knew that isupper() is redundant, but I retained it just to keep the
logic of the routine straightforward, (a tolower() on it's own would
need additional explanations to the OP).

Jul 3 '06 #20

P: n/a
"santosh" <sa*********@gmail.comwrites:
Keith Thompson wrote:
[...]
>Others have pointed out the redundancy of the isalpha() and isupper()
calls.

I knew that isupper() is redundant, but I retained it just to keep the
logic of the routine straightforward, (a tolower() on it's own would
need additional explanations to the OP).
IMHO it's an explanation that's well worth making. If you're going to
use tolower(), you should understand how it works; part of that is
understanding how it behaves with arguments for which isupper() is
false. (It's certainly easier to understand than the need (sometimes)
to cast the argument to unsigned char.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 3 '06 #21

P: n/a
Thx guys, really!
I've no idea what to do whithout you!
I have just made it!
Thx again my friend :)))

Jul 4 '06 #22

P: n/a
av
On Mon, 03 Jul 2006 12:21:55 -0500, Jack Klein <ja*******@spamcop.net>
wrote:
>#include <ctype.h>

char *(string_to_lower char *s)
{
char *ret = s;

if (s)
{
while (*s)
{
if (*s 0)
{
*s = tolower(*s);
}
++s;
}
}
return ret;
}
why not this?

char* to_lower(char* s)
{char *ret = s;
if(s) for( ;*s; ++s)
*s = tolower((unsigned char)*s);
return ret;
}

Jul 7 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.