String manipulations

I am having terrible trouble finding how to do something that seems rteally simple..

I am using the serial monitor to send a command to the Arduino to set the values of an RGB led. The commands are in the form of:

r255 g127 b23

I then get the program to split the above entered text into the 3 components using the strtok command :-

void splitString(char* data) {
  Serial.print("Data entered: ");
  Serial.println(data);
  char* parameter; 
  parameter = strtok (data, " ,");
  while (parameter != NULL) {
    setLED(parameter);
    parameter = strtok (NULL, " ,");
  }

which then calls the setLED function

I then find out if the first character of the split string is 'r', 'g', or 'b' using..

if (strncmp(data, "r", 1) == 0) { ...etc

Which is all very well.

However, the next bit I am stumpted.

Obviously if my text string entered into the serial monitor was 'r255 g127 b23' then the value of data in the setLED function will be 'r255'. I have checked the first character is valid by comparing it with r, g, or b. But now I want to take the last 1, 2 or 3 characters of the data variable, check it is valid and then turn it into a integer.

But how do I strip out the last 1, 2, or 3 characters after the 'r' ?

In other languages you might use the Right function as in Right(string,length). But how do I do this in C please?

Easier than you think!

if (strncmp(data, "r", 1) == 0) {
   value = atoi(data+1);
   ....

You can do validation (atoi doesn't) using strtol.

Excellent! Thanks for that. Works perfectly now.

Also,

strncmp(data, "r", 1) == 0

is better written

data[0] == 'r'

Of course - Makes perfect sense when you think about it. Thanks.