[Solved] String manipulation in C (replace & insert characters)


example by use strtok, strchr, sprintf

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

int main(){
    const char *data = "date=2013-12-09 time=07:31:10 d_id=device1 logid=01  user=user1 lip=1.1.1.1 mac=00:11:22:33:44:55 cip=2.2.2.2 dip=3.3.3.3 proto=AA sport=22 dport=11 in_1=eth1 out_1=";
    char *work = strdup(data);//make copy for work
    char *output = strdup(data);//allocate for output
    char *assignment; //tokenize to aaa=vvv
    size_t o_count = 0;//output number of character count
    for(assignment=strtok(work, " "); assignment ;assignment=strtok(NULL, " ")){
        o_count += sprintf(output + o_count, "%s#", strchr(assignment, '=')+1);
    }
    printf("%s", output);
    free(work);
    free(output);
    return 0;
}

11

solved String manipulation in C (replace & insert characters)