Pages: [1]   Go Down
Author Topic: Changing Case of char Array  (Read 854 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Bald-Headed, Middle-Aged Nerd
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 209
Posts: 13017
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


strupr
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Bald-Headed, Middle-Aged Nerd
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Bald-Headed, Middle-Aged Nerd
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Bob


strupr
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Bald-Headed, Middle-Aged Nerd
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 
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.


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Logged

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

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Whups! Where's my old dunce cap?
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
for (int i = 0; myString [i]; i++ )
  {
    myString[i] &= 0b11011111;
  }
Logged

"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.

Pages: [1]   Go Up
Jump to: