how to read a specific character from a char?

HELLO,

I am new with arduino and i want to know how to read a specific character from a char value that has 3 characters. I was searching in internet, but I just got information about the function String.charAt(), but what I want to do with my code, is to introduce characters numbers in the keypad, store them in a char funtion, and that the arduino can read them to do the correspondig procces.
If anyone know a form to do it as I said or even in other way please let me know to improve my ability in pragraming.

Thanks for the help.

1 Like

if using the String class you can use SetChatAt() to insert a char into a String

or you could use an array of char
https://www.arduino.cc/en/Reference/Array
http://www.cplusplus.com/doc/tutorial/ntcs/

Don't use Strings. Use zero terminated arrays of chars

char someChars[] = {"Hello"};

void setup()
{
  Serial.begin(115200);
  Serial.println(someChars);
  outputChars();
  someChars[2] = 'X';
  outputChars();
}

void loop()
{
}

void outputChars()
{
  for (int x = 0; x < strlen(someChars); x++)  //ignore the terminating zero
  {
    Serial.print(someChars[x]);
  }
  Serial.println();
}
  for (int x = 0; x < strlen(someChars); x++)  //ignore the terminating zero
  {
    Serial.print(someChars[x]);
  }

Or just

   Serial.print(someChars);

If someChars is NOT a string (i.e. is not NULL terminated), then strlen() can NOT be called.

PaulS:

  for (int x = 0; x < strlen(someChars); x++)  //ignore the terminating zero

{
    Serial.print(someChars[x]);
  }



Or just


Serial.print(someChars);




If someChars is NOT a string (i.e. is not NULL terminated), then strlen() can NOT be called.

...nor can Serial.print()

PaulS:

  for (int x = 0; x < strlen(someChars); x++)  //ignore the terminating zero

{
   Serial.print(someChars[x]);
 }



Or just


Serial.print(someChars);




If someChars is NOT a string (i.e. is not NULL terminated), then strlen() can NOT be called.

Paul - did you miss the title of this thread or the first sentence of the OPs first post ? He/she wants to access specific characters in an array of chars, hence my example which, by the way does include

  Serial.print(someChars);

I agree that I could have been more specific about the use of strlen() with arrays of chars

Paul - did you miss the title of this thread or the first sentence of the OPs first post ? He/she wants to access specific characters in an array of chars, hence my example which, by the way does include

I saw it. Your example assumes that someChars IS a string, because it calls strlen(). Your example DOES define a string, because you did not specify a size for the array. The string literal that you provided as the initializer has a terminating NULL, so the array was sized to include the terminating NULL.

If you want to show how to deal with an array of chars that is NOT a string, doing so with a string, and calling string functions, is NOT a good idea.

It is so simple to keep the array of chars NULL terminated that I can not imagine why anyone would NOT do so.

Your example assumes that someChars IS a string, because it calls strlen(). Your example DOES define a string, because you did not specify a size for the array. The string literal that you provided as the initializer has a terminating NULL, so the array was sized to include the terminating NULL.

All true and I have already commented on it in reply #5

What I don't understand is your reply #3 that implies that you had not read the thread title in that it fails to show how to read a character from an array of chars, commits the "sin" of using Serial.print() with the char array then has the nerve to suggest

If someChars is NOT a string (i.e. is not NULL terminated), then strlen() can NOT be called.

without mentioning the same problem with Serial.print()

We are evens on this I think and could both have been more specific in what we wrote.

UKHeliBob:
We are evens on this

a tempest in a teacup...

What I don't understand is your reply #3 that implies that you had not read the thread title in that it fails to show how to read a character from an array of chars, commits the "sin" of using Serial.print() with the char array then has the nerve to suggest
Quote

If someChars is NOT a string (i.e. is not NULL terminated), then strlen() can NOT be called.

without mentioning the same problem with Serial.print()

We are evens on this I think and could both have been more specific in what we wrote.

What I was trying to say was that if the argument passed to the function was a string, and that it was OK to call strlen() with that argument, it was also OK to call Serial.print() with that argument. It would not be necessary to print each character individually.

On the other hand, if the argument was NOT a string, so it was necessary to print the characters one by one, then one can't use strlen() to determine how many there are to print.