简体   繁体   中英

String split in C: separating the two parts

I have a string LOAD:07.09.30:-40.5&07.10.00:-41.7 incoming from a network.

I need to detect that it is a LOAD: type, then separate based on ' & ' (so I have 07.09.30:-40.5 first time)

And then separate 07.09.30 (keep it as string) and -40.5 (convert to float).

I am able to get the -40.5 float but can't find a way to store 07.09.30 as a string.

Below code shows output

tilt angle -40.50
tilt angle -41.70

How can I separate and store the 07.09.30 part?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
   char p[]="LOAD:07.09.30:-40.5&07.10.00:-41.7";
   char loadCmd[]="LOAD:";
   char data[]="";
   int ret;
   int len=strlen (p);
   int i=0, j=0;

   if (!(ret = strncmp(p, loadCmd, 5)))
   { 
      //copy p[5] to p[len-1] to char data[]
      for (i=5;i<len;i++){
         data[j++]=p[i];
      }
      data[j]='\0';
      char *word = strtok(data, "&");  //07.09.30:-40
      while (word != NULL){
         char *separator = strchr(word, ':');
         if (separator != 0){
            separator++;
            float tilt = atof(separator);
            printf("tilt angle %.2f\n", tilt);
         }
         word= strtok(NULL, "&");
      }
   }
   else {
      printf("Not equal\n");
   }
   return(0);
}

Before providing the solution, I would like to point out that the following code for storing/copying string is not encouraged.

char data[]="";
// other codes ...
for (i=5;i<len;i++){
         data[j++]=p[i];
      }

This will corrupt the memory in the stack. If you print out the value inside loadCmd after the above code, you would see what corruption I mean.

I would suggest allocate the required memory before copying the string. The following is one of the ways to assign memory (dynamically).

char *data = NULL;
// other codes ...
data = (char *)malloc((len-5+1)*sizeof(char));
// for-loop to copy the string

After this is changed, the solution will be straightforward. Just define an array of char inside the while-loop, and assign the characters in word one by one until hit ':'. An example is shown as follows.

 // inside the while-loop
         char first_part[20];
         i = 0;
         while (word[i] != ':')
         {
           first_part[i] = word[i];
           i++;
         }
         first_part[i] = '\0';
         printf("first part: %s\n", first_part);
         // the rest of the code ...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *storeUpto(char *store, const char *str, char ch){
    while(*str && *str != ch){
        *store++ = *str++;
    }
    *store = '\0';
    return (char*)(ch && *str == ch ? str + 1 : NULL);
}

int main (void){
    char p[] = "LOAD:07.09.30:-40.5&07.10.00:-41.7";
    char loadCmd[] = "LOAD:";
    char date1[9], date2[9], tilt[10];
    float tilt1, tilt2;

    if (!strncmp(p, loadCmd, 5)){
        char *nextp = storeUpto(date1, p + 5, ':');
        nextp = storeUpto(tilt, nextp, '&');
        tilt1 = atof(tilt);
        nextp = storeUpto(date2, nextp, ':');
        //storeUpto(tilt, nextp, '\0');
        tilt2 = atof(nextp);
        printf("date1:%s, tilt1:%.2f\n", date1, tilt1);
        printf("date2:%s, tilt2:%.2f\n", date2, tilt2);
    }
    else {
        printf("Not equal\n");
    }
    return(0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM