fork download
  1. #include <iostream>
  2. //using namespace std;
  3.  
  4. unsigned long s = 0x006600; // Green
  5. unsigned long t = 0x000066; // Blue ?
  6. unsigned long f = 0x000000; // Off
  7.  
  8. void parseData( char const * const data )
  9. {
  10. // try get a decimal number at the start of data
  11. char * p;
  12. uint8_t number = (uint8_t)strtoul( data, &p, 10 );
  13.  
  14. // if strtoul couldn't find a number at the start of data
  15. if ( p == data )
  16. {
  17. printf( "couldn't find a number!\n" );
  18. return;
  19. }
  20.  
  21. // else, p is now pointing at the character after the number
  22.  
  23. // so try increase p until the pointed character is a lowercase letter
  24. // (to skip spaces and anything else)
  25. while ( *p && ( *p < 'a' || *p > 'z' ) )
  26. {
  27. p++;
  28. }
  29.  
  30. char letter = *p;
  31.  
  32. // if a lowercase letter wasn't found
  33. if ( !letter )
  34. {
  35. printf( "couldn't find a letter!\n" );
  36. return;
  37. }
  38.  
  39. // else, the command is valid, a number and a lowercase letter have been found
  40.  
  41. printf( "number = %hhu, letter = %c\n", number, letter );
  42.  
  43. unsigned long color;
  44. switch ( letter )
  45. {
  46. case 's' :
  47. {
  48. color = s;
  49. break;
  50. }
  51. case 't' :
  52. {
  53. color = t;
  54. break;
  55. }
  56. default :
  57. {
  58. color = f;
  59. break;
  60. }
  61. }
  62.  
  63. printf( "color = 0x%06X\n", color );
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69. char command[] = "10 t";
  70. parseData( command );
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0s 5684KB
stdin
Standard input is empty
stdout
number = 10, letter = t
color = 0x000066