Fastest way to convert lower to upper case? 
Author Message
 Fastest way to convert lower to upper case?

Hi,

I was wondering what the fastest way is to convert a char array from
lower case to upper case. Right now I'm doing something like:

char Dummy[1024];

...

for (int i = 0; i < strlen(Dummy); i++)
{
  if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -= 32;

Quote:
}

Is there a better way to do this or even a library function I didn't
see?

Thanks for any comments!

Joachim

---------------------------------------------------------------------------

---------------------------------------------------------------------------



Thu, 25 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

   I was wondering what the fastest way is to convert a char array from
   lower case to upper case. Right now I'm doing something like:

   char Dummy[1024];

   ...

   for (int i = 0; i < strlen(Dummy); i++)
   {
     if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -= 32;
   }

   Is there a better way to do this or even a library function I didn't
   see?

#include <ctype.h>

char *cp;
for (cp = Dummy; *cp; cp++)
  *cp = toupper ((unsigned char) *cp);

Far more portable and probably faster too.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Thu, 25 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

Quote:

>Hi,

>I was wondering what the fastest way is to convert a char array
from
>lower case to upper case. Right now I'm doing something like:

>char Dummy[1024];

>...

>for (int i = 0; i < strlen(Dummy); i++)
>{
>  if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -=
32;
>}

>Is there a better way to do this or even a library function I
didn't
>see?

    toupper();
    strupr();

But...

These functions are not Standard, or something like that. They
should not see use in industrial-strength programs because they
don't recognize accented characters.

And 'strupr()' is faster than your loop statement. You are calling
'strlen()' over and over again!

--
 Phlip at politizen dot com                  (address munged)
======= http://users.deltanet.com/~tegan/home.html =======



Thu, 25 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?


   >Hi,
   >
   >I was wondering what the fastest way is to convert a char array
   from
   >lower case to upper case. Right now I'm doing something like:
   >
   >char Dummy[1024];
   >
   >...
   >
   >for (int i = 0; i < strlen(Dummy); i++)
   >{
   >  if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -=
   32;
   >}
   >
   >Is there a better way to do this or even a library function I
   didn't
   >see?

       toupper();
       strupr();

   These functions are not Standard, or something like that. They
   should not see use in industrial-strength programs because they
   don't recognize accented characters.

toupper() is standard C.  strupr() is not standard C.  I assume the
same goes for standard C++.

The characters that toupper() and family may vary based on the
current locale.
--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms
Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Thu, 25 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

   The characters that toupper() and family may vary based on the
   current locale.                         ^ result in
--
"Large amounts of money tend to quench any scruples I might be having."
  -- Stephan Wilms
Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Thu, 25 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

: Hi,

: I was wondering what the fastest way is to convert a char array from
: lower case to upper case. Right now I'm doing something like:

: char Dummy[1024];
: ...
: for (int i = 0; i < strlen(Dummy); i++)
: {
:   if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -= 32;
: }

  #include <ctype.h>

  char *pc, buffer[1024];

  for (pc = buffer ; *pc ; pc++)
     *pc = toupper(*pc);

is the easiest way if the buffer contains a string.

Will



Fri, 26 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

Quote:

> Hi,

> I was wondering what the fastest way is to convert a char array from
> lower case to upper case. Right now I'm doing something like:

> char Dummy[1024];

> ...

> for (int i = 0; i < strlen(Dummy); i++)
> {
>   if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -= 32;
> }

> Is there a better way to do this or even a library function I didn't
> see?

> Thanks for any comments!

> Joachim

> ---------------------------------------------------------------------------

> ---------------------------------------------------------------------------

There are two issues here.

First of all, you are calculating the length of the string once for
every iteration of the loop. There's nothing inside the loop that could
cause the string's length to change, therefore this is likely to be
inefficient. [ There may be some optimisers that could detect this but
I'd be inclined to avoid it. ]

Secondly, this is highly non-portable. It does not take into account the
character set in use at the locale in which the process is run. The
standard function toupper() is the routine to use.

Non-standard information...

Many platforms support a variation on toupper() known as _toupper(). If
this is a available, it will invariably be implemented as a macro and
will usually be faster than the principle function. DANGER, DANGER! All
implementations of _toupper() that I've seen rely on the fact that the
character being converted is actually a "real" character. For example,
if you pass '\n' to _toupper() you'll probably get a silly result. If
you pass '\n' to toupper() it'll come back as '\n'.

--
Andy Knight

void main() detected - core dumped.



Fri, 26 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

Quote:
> Hi,

> I was wondering what the fastest way is to convert a char array from
> lower case to upper case. Right now I'm doing something like:

> char Dummy[1024];

> ...

> for (int i = 0; i < strlen(Dummy); i++)
> {
>   if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -= 32;
> }

> Is there a better way to do this or even a library function I didn't
> see?

You have to consider how portable your code is going to be.  If you consider
that it might be necessary to support Unicode at some point, then you would
need to consider three different case transformations...

        lower to upper case
        upper to lower case
        upper/lower to title case

Title case is where the first character is upper case and the remaining
characters are lower case.  It is not quite as trivial as it sounds because
there are some Unicode characters which are really two characters squeezed
together.  For example, "ae" is a single character, so it has the cases...

        lower: "ae"
        upper: "AE"
        title" "Ae"

all as single characters.

If you make the assumption that Unicode support might be necessary, then you
will want to use a function to make the conversions and the function will
almost certainly work via table lookup (although I have seen other
algorithms, but they are not simple) because you can't assume that any
particular character has all of the three possible cases, and even so, the
"distance" between lower and upper case is not fixed for all characters.

It also matters what operating system you are using and which might be a
possible target for future support.  NT has some of these functions built
in.  There is also some support under OS/2.  Possibly Plan/9 or some of the
other systems have some support, but basically it might be something that
you need to write yourself using the Unicode standard (files are available
online from www.unicode.org) to generate your tables.  Remember that the
Unicode standard is evolving, current at level 2.1 with work proceeding on
3.0.

--
Michael Lee Finney



Fri, 26 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?
[snip]

Quote:
>   #include <ctype.h>

>   char *pc, buffer[1024];

>   for (pc = buffer ; *pc ; pc++)
>      *pc = toupper(*pc);

> is the easiest way if the buffer contains a string.

> Will


Is

for (pc = buffer ; *pc = toupper(*pc) ; pc++);

even faster?

Sven



Sat, 27 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

Quote:


>>   for (pc = buffer ; *pc ; pc++)
>>      *pc = toupper(*pc);
>Is

>for (pc = buffer ; *pc = toupper(*pc) ; pc++);

>even faster?

In egcs, with no optimization, optimization one, and optimization two,
the second version has a leaner assembly file, so I imagine it is
faster.  With optimization three, there's no difference in the
assembly code.

--
----------------------------------

----------------------------------



Sat, 27 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

Quote:


> [snip]
> >   #include <ctype.h>

> >   char *pc, buffer[1024];

> >   for (pc = buffer ; *pc ; pc++)
> >      *pc = toupper(*pc);

> > is the easiest way if the buffer contains a string.

> > Will

> Is

> for (pc = buffer ; *pc = toupper(*pc) ; pc++);

> even faster?

Why will it be faster ?
The Will Rose's code involves one less call to toupper than
your code. (*pc != 0 will be checked for the terminating null
character, only after the call to toupper happens)

Quote:

> Sven

--
cheers,
Shiva
comp.lang.c++ FAQ:http://www.cerfnet.com/~mpcline/c++-faq-lite/

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sat, 27 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

: [snip]
:>   #include <ctype.h>
:>
:>   char *pc, buffer[1024];
:>
:>   for (pc = buffer ; *pc ; pc++)
:>      *pc = toupper(*pc);
:>
:> is the easiest way if the buffer contains a string.
:>
: Is

: for (pc = buffer ; *pc = toupper(*pc) ; pc++);

: even faster?

Pass.  I wouldn't be suprised if a compiler produced the same
code for both.

Will



Sat, 27 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

Quote:


>    I was wondering what the fastest way is to convert a char array
from
>    lower case to upper case. Right now I'm doing something like:

>    char Dummy[1024];

>    ...

>    for (int i = 0; i < strlen(Dummy); i++)
>    {
>      if (((int)Dummy[i] <= 122) && ((int)Dummy[i] >= 97)) Dummy[i] -=
32;
>    }

>    Is there a better way to do this or even a library function I
didn't
>    see?

> #include <ctype.h>

> char *cp;
> for (cp = Dummy; *cp; cp++)
>   *cp = toupper ((unsigned char) *cp);

Except that I believe the behaviour of toupper is undefined if the
character is not a letter.

--
Jim
I ignore all email from recruitment agencies.
Please do not send me email with questions - post
here.

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sat, 27 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?

Quote:



>> >   for (pc = buffer ; *pc ; pc++)
>> >      *pc = toupper(*pc);
>> for (pc = buffer ; *pc = toupper(*pc) ; pc++);
>Why will it be faster ?
>The Will Rose's code involves one less call to toupper than
>your code. (*pc != 0 will be checked for the terminating null
>character, only after the call to toupper happens)

Both code have one call to toupper in each iteration of the for loop.
Sven's code has a call to toupper even for the null char, whereas Rose's
code does not.
But Roses's code may have the compiler compute *pc twice, whereas Sven's
code does not do this.  Consider
   void f(char * text) {
      char& silly=text[1];
      for (char * pc=text; *pc; ++pc) {
         silly='x';
         *pc=toupper(*pc);
      }
   }
Here the value of *pc in the condition part of the for loop and the
body of the for loop may be different.  So *pc will have to be
recomputed twice.  Without the "silly='x'" and strong optimization
from the compiler, both versions should be the same.

--
----------------------------------

----------------------------------



Sat, 27 Oct 2001 03:00:00 GMT  
 Fastest way to convert lower to upper case?


Quote:

> >   *cp = toupper ((unsigned char) *cp);
> Except that I believe the behaviour of toupper is undefined if the
> character is not a letter.

It's defined if the int argument can be represented as
an unsigned char, or is EOF; it must return its argument
unchanged unless it's a lower case letter, which is
converted to the corresponding upper case letter.
(If there is no corresponding upper case letter, I think
it must return the argument unchanged even for lower
case letters.)

--
MJSR

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sat, 27 Oct 2001 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. moving upper case names to lower case

2. Upper case / lower case problem in IDL file

3. Upper and lower case words

4. Select returning UPPER & LOWER CASE

5. STL string to upper/lower case

6. upper/lower case with VMS C

7. Convert keypress to upper case

8. convert string to Upper case

9. Converting Strings to Upper Case

10. string -- convert to lower case

11. Converting to lower case?

12. Converting a string to lower case?

 

 
Powered by phpBB® Forum Software