Shop OBEX P1 Docs P2 Docs Learn Events
converting strings — Parallax Forums

converting strings

I was on a roll this morning and now I'm stumped. I have several "if" statements that I need to compare a known variable to an incoming string. I believe my problem is that I need to convert the string to an array before the if statement? any thoughts? the first print command works perfect I get MAIN or VIC1 but everything I have tried never gets to the second print command.
char  rxchar = 0;                    // 
                 int   i = 0;
                 char string[4];
             
                fdserial_rxFlush(lcd);                   // flush rx buffer

            while(rxchar != '\r' && rxchar != '\n') { // no CR or LF
               rxchar = fdserial_rxChar(lcd);         // get char
               string[i] = rxchar;                     // append char
               i++;
               }
             string[i] = '\0';                           // append NULL 
             
                pause(100);
                print("name= %s\n",string);  
                






       if (string == "MAIN") {       // 33333  MAIN 
       
           print("inside\n");
 
           }

Comments

  • You can't directly compare strings in C (using ==). You have to use a library function (strcmp).

    if (strcmp(string,"MAIN") == 0) { print("got MAIN\n"); }

    I think you have to include the declarations from <string.h>
  • As Mike mentioned, you can use 'strcmp', but if you do not want to add 'string.h' to your code, then you can do something as follows. This shows an example of using strcmp as well as a function that compares the strings without 'string.h'.
    #include <stdio.h>
    #include <string.h>
    
    char *MAIN = "MAIN";
    char my_string[5] = "MAIN";
    int compare_string(char*, char*);
    
    int  main()
    {
        int result = 0;
        if (strcmp(MAIN, my_string) == 0)
            printf("Inside\n");
    
    
        // No string.h option
        result = compare_string(MAIN, my_string);
        printf("Result = %d\n", result);
        if (result == 0)
            printf("Also Inside\n");
        else
            printf("Not Inside\n");
    }
    
    int compare_string(char *first, char *second)
    {
       while (*first == *second) {
          if (*first == '\0' || *second == '\0')
              break;
    
          first++;
          second++;
       }
    
       if (*first == '\0' && *second == '\0')
          return 0;
       else
          return -1;
    }
    
  • As another alternative, you could add the ASCII value of each incoming character to an accumulator and check that: "M"+"A"+"I"+"N" = 293. Saves you from having to store characters, but introduces the possibility of other strings having the same value.
  • ChrisGadd wrote: »
    As another alternative, you could add the ASCII value of each incoming character to an accumulator and check that: "M"+"A"+"I"+"N" = 293. Saves you from having to store characters, but introduces the possibility of other strings having the same value.

    If you use a better checksum (like a CRC) than just a simple sum, you decrease the chances of other strings having the same value. But you should really just compare the original strings using strcmp() or something equivalent.

    The only case I can think of where relying on a checksum actually makes sense is if you need to test pairs of vast quantities of long strings for equality (in which case you'd use a real cryptographic checksum like SHA-1 or SHA-256).
  • JonM wrote: »
    As Mike mentioned, you can use 'strcmp', but if you do not want to add 'string.h' to your code, then you can do something as follows. This shows an example of using strcmp as well as a function that compares the strings without 'string.h'.
    #include <stdio.h>
    #include <string.h>
    
    char *MAIN = "MAIN";
    char my_string[5] = "MAIN";
    int compare_string(char*, char*);
    
    int  main()
    {
        int result = 0;
        if (strcmp(MAIN, my_string) == 0)
            printf("Inside\n");
    
    
        // No string.h option
        result = compare_string(MAIN, my_string);
        printf("Result = %d\n", result);
        if (result == 0)
            printf("Also Inside\n");
        else
            printf("Not Inside\n");
    }
    
    int compare_string(char *first, char *second)
    {
       while (*first == *second) {
          if (*first == '\0' || *second == '\0')
              break;
    
          first++;
          second++;
       }
    
       if (*first == '\0' && *second == '\0')
          return 0;
       else
          return -1;
    }
    
    Why would want to avoid using string.h? The strcmp in the C library is probably smaller than the one you present as well as faster.

  • David Betz wrote: »
    Why would want to avoid using string.h? The strcmp in the C library is probably smaller than the one you present as well as faster.

    This was only an example and offered as an alternative not a must do. If it does not fit for the particular scenario then one should do something else.
    However, in other embedded situations I've been involved with libs such as 'string.h' were not available so a manual workaround had to be used.
    Again, it was offered as an alternative and example.
  • JonM wrote: »
    David Betz wrote: »
    Why would want to avoid using string.h? The strcmp in the C library is probably smaller than the one you present as well as faster.

    This was only an example and offered as an alternative not a must do. If it does not fit for the particular scenario then one should do something else.
    However, in other embedded situations I've been involved with libs such as 'string.h' were not available so a manual workaround had to be used.
    Again, it was offered as an alternative and example.
    Fair enough. I only mentioned this because sometimes people think that including something like <string.h> will pull in lots of stuff they don't need. While this is true of some Spin compilers, it is not true of C. If you include a library you only pull functions out of the library that your program actually needs.

  • David Betz wrote: »
    Fair enough. I only mentioned this because sometimes people think that including something like <string.h> will pull in lots of stuff they don't need. While this is true of some Spin compilers, it is not true of C. If you include a library you only pull functions out of the library that your program actually needs.

    It's a bit off topic of the post, however with regards to whether a compiler would add non used parts of string.h in the compiled code depends on the linker that is used. If the compiler has a lazy linker, then it is possible that all of what is in something such as 'string.h' could be added to the code. Also, if the included code contains static or inline defines it is possible that these could be added to the code even if they are not used.

    It would be nice if in 'C' there was the option to include portions of the include like with Python.
    Something like:
    from string.h include strcmp

    This way there would be no question as to what is added to the compiled code. Well, outside any baggage strcmp may carry with it.
  • The <string.h> file usually does not include the definitions of the functions, just prototypes. Each function is generally in a separate file in a library so only that function is included to resolve the reference to its name. Also, the GCC linker is able to extract a single function from a file containing multiple functions as well. This requires a special linker option though. I suppose there might be some very dumb C compilers out there that pull in an entire library for a single function reference but I don't know of any.
Sign In or Register to comment.