Changing Case of char Array

I've read a lot about how some users "hate" the String Object and prefer to use char arrays when possible. So I'm trying to become more proficient in using char arrays to store my strings.

Question:

What would the best way be to change the case of a string stored as a char array to Upper Case.
I see that there are already methods like strcmp and strcpy to perform other tasks on char arrays.
I can't seem to find something for changing case ( as exists for the String Object ).

Would appreciate any guidance on this.

Thank you.

Bob W

strupr

1 Like

For the ASCII alphas, bit 5 is the difference in case.

1 loop the length of the buffer with break on NULL, if the ASCII is not alpha-only then with a range check ( data & 0xDF >= 'A' ) && ( data & 0xDF <= 'Z' ) and either set or clear bit 5.... make the state an arg and 1 function could do both change to upper or lower case.

And you don't need string.h to do that.

That worked good !
I like it.
Thanks for helping me !

Bob

GoForSmoke:
For the ASCII alphas, bit 5 is the difference in case.

1 loop the length of the buffer with break on NULL, if the ASCII is not alpha-only then with a range check ( data & 0xDF >= 'A' ) && ( data & 0xDF <= 'Z' ) and either set or clear bit 5.... make the state an arg and 1 function could do both change to upper or lower case.

And you don't need string.h to do that.

This is certainly easier.
I don't know why I couldn't find it myself.
Thanks you !

Bob

char array strings are so simple and easy to get at I have to wonder at the use of String objects that 'protect' the data from the programmer. What kind of programmers does data need protection from? Can't be -good- ones!

Mr. "Smoke":

Below is how I tested your suggestion to UpCase a string.
Works good.
Doesn't look very good.
Can you suggest how I can get rid of the for loop and replace it with a while (myString != NULL) ?
I don't see how to step through the buffer without an index like "i".

Thanks.
Bob W.

char myString[] = "Hello";

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 100; i++ )  // 100 set arbitrarily and excessively large
  {
    if ( myString[i] == NULL ) break;
    myString[i] = myString[i] & 0b11011111;
  }

  Serial.print( &myString[0] );

GoForSmoke:
For the ASCII alphas, bit 5 is the difference in case.

1 loop the length of the buffer with break on NULL, if the ASCII is not alpha-only then with a range check ( data & 0xDF >= 'A' ) && ( data & 0xDF <= 'Z' ) and either set or clear bit 5.... make the state an arg and 1 function could do both change to upper or lower case.

And you don't need string.h to do that.

  for (int i = 0; i < 100; i++ )  // 100 set arbitrarily and excessively large

Replace the 100 with strlen(myString) and delete the comment.

And you can remove the if test.

As long as I'm gonna do it, I'll use a pointer and stick with hex.

char myString[] = "Hello Again";
char *ch;

void setup() {
  Serial.begin(9600);

  ch = myString;
  while (*ch) // exits on terminating NULL
  {
    if (( *ch &0xDF >= 'A' ) && ( *ch & 0xDF <= 'Z' ))  *ch &= 0xDF; // so that bit 5 only gets cleared for alphas 
  }

  Serial.println( myString );
}

GoForSmoke:

  while (*ch) // exits on terminating NULL

{
    if (( *ch & 0xDF >= 'A' ) && ( *ch & 0xDF <= 'Z' ))  *ch &= 0xDF; // so that bit 5 only gets cleared for alphas

// this was left as an exercise for the student
    ch++;
  }

Whups! Where's my old dunce cap?

Doesn't look very good.
Can you suggest how I can get rid of the for loop and replace it with a while (myString != NULL) ?

 for (int i = 0; myString [i]; i++ )
  {
    myString[i] &= 0b11011111;
  }