-5

I am very new in C, I have little idea about sprintf but I can't fulfill my requirement.

I have a char * variable which contains string like below :

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=

I want an output as

2013-12-09#07:31:10#device1#01#user1#1.1.1.1#00:11:22:33:44:55#2.2.2.2#3.3.3.3#AA#22#11#eth1##

if some value is null after = it should print ## in sequence.

Constantin
  • 8,721
  • 13
  • 75
  • 126
Jatin Bodarya
  • 1,425
  • 2
  • 20
  • 32

2 Answers2

1

I am not going to give you exact code but I will give you some links that will help you.

strchr :: You can use this find the position of '=' in the string.

  1. Now, copy the string after the position of '=' till you find a 'space'.
  2. Whenever you will find a 'space', write a '#' in the buffer.
  3. Keep doing this, till you encounter a '\0'. Write '##' to buffer when you have encountered '\0'
  4. Append that with a '\0'.

Ex:: C function strchr - How to calculate the position of the character?

Community
  • 1
  • 1
Abhineet
  • 5,320
  • 1
  • 25
  • 43
0

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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • for(assignment=strtok(work, " "); assignment ;assignment=strtok(NULL, " ")){ o_count += sprintf(output + o_count, "%s#", strchr(assignment, '=')+1); } this line gives segfault sometimes !! – Jatin Bodarya Dec 09 '13 at 10:40
  • @user95711 There is no problem, but do not or directly manipulating the string? – BLUEPIXY Dec 09 '13 at 10:56
  • o_count += sprintf(output + o_count, "%s#", strchr(assignment, '=')+1); there is something wrong is happening here. n yes I am doing the same as you suggested. – Jatin Bodarya Dec 09 '13 at 10:57
  • @user95711 It becomes the premise that contains the '=' always the token, but the format is or has become so? It is necessary that you want to skip the token by checking in advance because it causes a segmentation error as long as there are cases where it is not included is if '='. – BLUEPIXY Dec 09 '13 at 10:58
  • @user95711 Is it possible to present the data in the case of cause an error? – BLUEPIXY Dec 09 '13 at 11:00
  • see the strace in your answer – Jatin Bodarya Dec 09 '13 at 11:05
  • @user95711 There is no problem here. Segment fault does not occur. I think that it's because of data to be handled. – BLUEPIXY Dec 09 '13 at 11:09
  • @user95711 Segment fault error happens by compiling and run my code? Please show the code if only as occur in your code. – BLUEPIXY Dec 09 '13 at 11:17
  • well actually I am using this code with open source ulogd 2.0 . in file ulogd_output_SYSLOGMU.c if I remove this line there is no segfault. – Jatin Bodarya Dec 09 '13 at 11:24
  • @user95711 It is a meaningless story. – BLUEPIXY Dec 09 '13 at 11:30
  • don't have you reset the o_count ? – BLUEPIXY Dec 09 '13 at 13:18