Upper Case 
Author Message
 Upper Case

Hi,

Does anyone here know of a small function or command in C that will tranform
a string into all upper or lower case?

I want to make the contents of a variable in all caps if the user who input
the string, didn't have caps lock on.

Hope someone can help.



Fri, 13 Jun 2003 01:08:00 GMT  
 Upper Case

Quote:

> Does anyone here know of a small function or command in C that will tranform
> a string into all upper or lower case?

Use tolower() or toupper() in a loop across all the characters in
the string.
--
"What is appropriate for the master is not appropriate for the novice.
 You must understand the Tao before transcending structure."
--The Tao of Programming


Fri, 13 Jun 2003 01:09:29 GMT  
 Upper Case

Quote:
>Does anyone here know of a small function or command in C that will

tranform

There is no command in C. Only function calls.

Quote:
>a string into all upper or lower case?

>I want to make the contents of a variable in all caps if the user who input
>the string, didn't have caps lock on.

There is no standard function to do that, but you can use a loop and the
toupper() function from <ctype.h> on each character.

Important note:
If you want to uppercase the original string (rather than a copy), this
string must be modifiable. Keep in mind that string litterals are not
guaranteed to be modifiable. (I declare them const to prevent that kind of
bug, it makes my compiler scream on programming error).

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Fri, 13 Jun 2003 01:21:20 GMT  
 Upper Case

Quote:


> >Does anyone here know of a small function or command in C that will
> tranform

> There is no command in C. Only function calls.

Commands in other languages probably map more accurately to C
statements than function calls.
--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms


Fri, 13 Jun 2003 01:40:23 GMT  
 Upper Case

Quote:

> Hi,

> Does anyone here know of a small function or command in C that will tranform
> a string into all upper or lower case?

> I want to make the contents of a variable in all caps if the user who input
> the string, didn't have caps lock on.

> Hope someone can help.

#include <ctype.h>

void xstrupr(char *s)
{
    while (*s) {
        *s = (char)(toupper((unsigned char)(*s)));
        s++;
    }

Quote:
}

Later, Gregory Pietsch


Fri, 13 Jun 2003 01:56:02 GMT  
 Upper Case

Quote:

> void xstrupr(char *s)
> {
>     while (*s) {
>         *s = (char)(toupper((unsigned char)(*s)));

More simply:
        *s = toupper ((unsigned char) *s);
Quote:
>         s++;
>     }
> }

> Later, Gregory Pietsch



Fri, 13 Jun 2003 02:11:51 GMT  
 Upper Case
Thanks!!
I just implemented the code into my program for an array and a pointer and
both worked great.


Quote:

> > void xstrupr(char *s)
> > {
> >     while (*s) {
> >         *s = (char)(toupper((unsigned char)(*s)));

> More simply:
> *s = toupper ((unsigned char) *s);

> >         s++;
> >     }
> > }

> > Later, Gregory Pietsch



Fri, 13 Jun 2003 06:56:57 GMT  
 Upper Case
Hi, you may also try it this way:
void string_my_way (char *s, char (*char_convert)(int))
{
  while (*s)
  {
    *s = char_convert((unsigned char) *s);
    s++;
  }

Quote:
}

This way you call the function like this:
string_my_way(my_string, toupper);
to make the whole string upper case, or call it with "tolower" to make
it lower case.

I think that's how you accept a function pointer, but you may have to
change it a little.



Quote:
> Thanks!!
> I just implemented the code into my program for an array and a
pointer and
> both worked great.




> > > void xstrupr(char *s)
> > > {
> > >     while (*s) {
> > >         *s = (char)(toupper((unsigned char)(*s)));

> > More simply:
> > *s = toupper ((unsigned char) *s);

> > >         s++;
> > >     }
> > > }

> > > Later, Gregory Pietsch

Sent via Deja.com
http://www.deja.com/


Fri, 13 Jun 2003 08:40:27 GMT  
 Upper Case


Quote:
>Hi, you may also try it this way:
>void string_my_way (char *s, char (*char_convert)(int))
>{
>  while (*s)
>  {
>    *s = char_convert((unsigned char) *s);
>    s++;
>  }
>}

>This way you call the function like this:
>string_my_way(my_string, toupper);
>to make the whole string upper case, or call it with "tolower" to make
>it lower case.

You'd be invoking undefined behavior, since tolower returns int, but your
function pointer type has a char return value, which is easy to fix.  Other
than that it's a fine idea. It might be better in some cases to have an
interfaces that takes a separate source and target buffer.

Another way to do it is to make a macro which defines the functions that
you need:

#include <ctype.h>

#define ST_DEF(FUNC)                                           \
    static void st_ ## FUNC (char *dest, const char *src)      \
    {                                                          \
        while (*src)                                           \
            *dest++ = FUNC((unsigned char) *src++);            \
    }

Then you just write:

    CTYPE_ST_DEF(toupper)
    CTYPE_ST_DEF(tolower)

which creates the functions st_toupper() and st_tolower(). :)



Fri, 13 Jun 2003 09:22:05 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. moving upper case names to lower case

2. Upper case / lower case problem in IDL file

3. Convert keypress to upper case

4. Fastest way to convert lower to upper case?

5. convert string to Upper case

6. Converting Strings to Upper Case

7. testing 'string' for upper-case letters

8. Change a foreign language string to upper case

9. Upper case

10. change char in edit box to upper case as it is typed

11. CPP macro names, always upper case ?

12. Edit Controll and Upper case?

 

 
Powered by phpBB® Forum Software