Home > OS >  Remove double-double quotes in a string in C
Remove double-double quotes in a string in C

Time:07-08

I have the string below which i have tried to remove one of the double quotes.

MSNT_SystemTrace,RecvIPV4,FFFFFFFF,FFFFFFFF,0,2,0,,,,0,0x0,"[2]FFFFFFFF.FFFFFFFF::07/02/2022-10:32:37.367 [MSNT_SystemTrace]{""PID"":9104,""size"":361,""daddr"":""52.149.21.60"",""saddr"":""192.168.1.52"",""dport"":443,""sport"":51799,""seqnum"":0,""meta"":{""provider"":""MSNT_SystemTrace"",""event"":""RecvIPV4"",""time"":""2022-07-02T10:32:37.367"",""cpu"":2,""task"":""TcpIp""}}"
{""PID"":9104,""size"":361,""daddr"":""52.149.21.60"",""saddr"":""192.168.1.52"",""dport"":443,""sport"":51799,""seqnum"":0,""meta"":{""provider"":""MSNT_SystemTrace"",""event"":""RecvIPV4"",""time"":""2022-07-02T10:32:37.367"",""cpu"":2,""task"":""TcpIp""}}"

Here is my code;

char* newString;
int i, j;
if (substring[0] != '"')
    newString[0] = substring[0];
for (i = j = 1; i < strlen(substring); i  ) {
    if (substring[i] == '"')
        continue;
    newString[j  ] = substring[i];
}

But what i end up getting is a string without any of the quotes like the one below;

{PID:9104,size:361,daddr:52.149.21.60,saddr:192.168.1.52,dport:443,sport:51799,seqnum:0,meta:{provider:MSNT_SystemTrace,event:RecvIPV4,time:2022-07-02T10:32:37.367,cpu:2,task:TcpIp}}

The string below is what i want to achieve, how can i go about this?

{"PID":9104,"size":361,"daddr":"52.149.21.60","saddr":"192.168.1.52","dport":443,"sport":51799,"seqnum":0,"meta":{"provider":"MSNT_SystemTrace","event":"RecvIPV4","time":"2022-07-02T10:32:37.367","cpu":2,"task":"TcpIp"}}

CodePudding user response:

You check the current character for a double quote and jump accordingly, in order to get what you want, you must also check the next character:

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

int main()
{
    const char *substring = "{\"\"PID\"\":9104,\"\"size\"\":361,\"\"daddr\"\":\"\"52.149.21.60\"\",\"\"saddr\"\":\"\"192.168.1.52\"\",\"\"dport\"\":443,\"\"sport\"\":51799,\"\"seqnum\"\":0,\"\"meta\"\":{\"\"provider\"\":\"\"MSNT_SystemTrace\"\",\"\"event\"\":\"\"RecvIPV4\"\",\"\"time\"\":\"\"2022-07-02T10:32:37.367\"\",\"\"cpu\"\":2,\"\"task\"\":\"\"TcpIp\"\"}}";

    int i, j = 0, len = strlen(substring);
    char* newString = (char*)malloc(len 1);

    if (newString == NULL)
    {
        printf("malloc failed: %s\n", strerror(errno));
        return 1;
    }

    for (i = 0; i < len; i  ) 
    {
        if (substring[i] == '"' && i   1 < len && substring[i   1] == '"')
            continue;

        newString[j  ] = substring[i];
    }


    newString[j] = '\0';    
    printf("%s\n", newString);

    free(newString);
    return 0;
}

P.S. This code will remove all consecutive double-quotes and leave only one instead. But from your question, I believe your goal is to get a valid JSON string, so it should be fine.

CodePudding user response:

When you encounter a quote, you'll need to check whether the previous character is a quote or not. Might not be the most elegant solution, but this should work:

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

int main()
{
    char* substring = "{\"\"PID\"\":9104,\"\"size\"\":361,\"\"daddr\"\":\"\"52.149.21.60\"\",\"\"saddr\"\":\"\"192.168.1.52\"\"}";
    int length = strlen(substring);
    char* new_string = calloc(length   1, sizeof(char));
    int new_index = 0;
    
    for (int i = 0; i < length; i  ) {
        if (substring[i] == '"' && substring[i   1] == '"') continue;
        new_string[new_index  ] = substring[i];
    }
    
    printf("%s\n", new_string);
    // {"PID":9104,"size":361,"daddr":"52.149.21.60","saddr":"192.168.1.52"}
    free(new_string);

    return 0;
}

Note that the double quotes need to be escaped (\") for this to work, otherwise each pair of double quotes just signifies a new string, such that "{""PID" is the same as "{" and "PID".

CodePudding user response:

If the string is modifiable, the changes can be made in place.
If the string is not modifiable, a copy could be made and the copy passed to the function.

#include <stdio.h>

void pairs ( char *str, int pair) {
    char *from = str; // assign str to pointer
    char *to = str; // addign str to pointer

    while ( from && *from) { // not NULL and not terminating zero
        if ( pair == *from) { // characters match
            *to = *from; // assign character
              to; // advance pointer
              from; // advance pointer
            if ( pair == *from) { // a second match
                  from; // only advance one pointer
            }
        }
        else {
            *to = *from; // assign character
              to; // advance pointer
              from; // advance pointer
        }
    }
    *to = 0; // zero terminate
}

int main ( void) {
    char quotes[] =
"MSNT_SystemTrace,RecvIPV4,FFFFFFFF,FFFFFFFF,0,2,0,,,,0,0x0"
",\"[2]FFFFFFFF.FFFFFFFF::07/02/2022-10:32:37.367 [MSNT_SystemTrace]"
"{\"\"PID\"\":9104,\"\"size\"\":361,\"\"daddr\"\":\"\"52.149.21.60\"\",\"\"saddr\"\":"
"\"\"192.168.1.52\"\",\"\"dport\"\":443,\"\"sport\"\":51799,\"\"seqnum\"\":0,\"\""
"meta\"\":{\"\"provider\"\":\"\"MSNT_SystemTrace\"\",\"\"event\"\":\"\"RecvIPV4"",\"\""
"time\"\":\"\"2022-07-02T10:32:37.367\"\",\"\"cpu\"\":2,\"\"task\"\":\"\"TcpIp\"\"}}\"";

    printf ( "%s\n\n", quotes);
    pairs ( quotes, '\"');
    printf ( "%s\n", quotes);
}
  •  Tags:  
  • c
  • Related