This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Creating a string from a character array

Hi all,

I would like to use the strcmp - string compare function, however don't know how to create a string from character array. Standard way would be something like this:

char arr[ ] = "Hello World";
string str(arr);

However the compiler doesn't like the type string. Is there a way to create a string that I could use such as in the following example?

if(strcmp("Hello World",str) == 0)
{do something}

Regards

Milan

Parents
  • Hi Milan, perhaps your C platform is buggy, or maybe there's something else going on in your test case, but strcmp should not tell you a string matches an initial sub-string.

    Given a compliant strcmp implementation (as in John's example, or the standard C library), the code snippet:

    char buf[BUFSZ];
    
    strcpy (buf, "rat");
    (void) printf ("%s %s rate\n", buf, (strcmp (buf, "rate")) ? "!=" : "==");
    strcpy (buf, "rate");
    (void) printf ("%s %s rate\n", buf, (strcmp (buf, "rate")) ? "!=" : "==");
    strcpy (buf, "rated");
    (void) printf ("%s %s rate\n", buf, (strcmp (buf, "rate")) ? "!=" : "==");
    

    should produce:

    rat != rate
    rate == rate
    rated != rate
    

    The return 0 in the strcmp example John posted is hit only when *s1 == *s2 and *s1 == 0, so *s2 == 0 as well. The for loop exits on the first difference and returns non-zero, so regardless of operand order that code will return 0 only when the two strings are identical.

  • Thank you all for contribution I think the strcmp() is clear now however my problem still persists. I will try to explain so hopefully somebody will be able to help.

    I am using the nRF51-dk and in this case original unmodified experimental_ble_app_uart.

    The only change that I have made to the code is the following to trigger LED when string matches:

    void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
      for (int i = 0; i < length; i++)
      {
          simple_uart_put(p_data[i]);
      }
    
    simple_uart_put('\n');
    
    if (strcmp("rate", (const char*)p_data) == 0){
      LEDS_INVERT(BSP_LED_1_MASK);
      }
    }
    

    What I am getting is rather surprising.

    1.When I type in "rat" the LED is not inverted

    2.Then typing in "rate" LED is inverted

    3.Then typing "rat" LED is surprisingly inverted

    4.At this point LED is inverted every time "r" "ra" "rat" "rate" is sent

    5

    5.1. After typing any 4 letters etc. "abcd" LED is not inverted but after that only "rate" can invert LED

    5.2 After typing any 5 letters nothing can invert LED not even "rate"

    Anyone could give an advise how to make this robust?

    Regards

    Milan

Reply
  • Thank you all for contribution I think the strcmp() is clear now however my problem still persists. I will try to explain so hopefully somebody will be able to help.

    I am using the nRF51-dk and in this case original unmodified experimental_ble_app_uart.

    The only change that I have made to the code is the following to trigger LED when string matches:

    void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
      for (int i = 0; i < length; i++)
      {
          simple_uart_put(p_data[i]);
      }
    
    simple_uart_put('\n');
    
    if (strcmp("rate", (const char*)p_data) == 0){
      LEDS_INVERT(BSP_LED_1_MASK);
      }
    }
    

    What I am getting is rather surprising.

    1.When I type in "rat" the LED is not inverted

    2.Then typing in "rate" LED is inverted

    3.Then typing "rat" LED is surprisingly inverted

    4.At this point LED is inverted every time "r" "ra" "rat" "rate" is sent

    5

    5.1. After typing any 4 letters etc. "abcd" LED is not inverted but after that only "rate" can invert LED

    5.2 After typing any 5 letters nothing can invert LED not even "rate"

    Anyone could give an advise how to make this robust?

    Regards

    Milan

Children
No Data
Related