Go Down

Topic: Changing Case of char Array (Read 911 times) previous topic - next topic

BobW

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

Coding Badly


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.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

BobW

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

Bob


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.



BobW

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

Bob



strupr

GoForSmoke

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!

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

BobW

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.

Code: [Select]
 
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] );




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.



PaulS

Code: [Select]
 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.

GoForSmoke

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

Code: [Select]
 
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 );
}



I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

spatula


Code: [Select]

  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++;
  }





GoForSmoke

Whups! Where's my old dunce cap?
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

AWOL

Quote
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) ?

Code: [Select]
for (int i = 0; myString [i]; i++ )
  {
    myString[i] &= 0b11011111;
  }
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up