0

I'm trying to find different substrings of information from a larger string where the information is separated by ":"

For example: username:id:password:info

How could I using strstr function in C loop this string to extract the information into different substring?

So far I've tried something like:

char string[] = "username:id:password:info"
char *endptr;

if((endptr = strstr(string, ":")) != NULL)  {
  char *result = malloc(endptr - string + 1);
  if(result != NULL) {
    memcpy(result, string, (endptr - string));
    result[endptr - string] = '\0';
    username = strdup(result);
    free(result);
  }
}

I want to make this loopable to extract all substrings.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
renshencha
  • 55
  • 6
  • 2
    `strstr` is not the right tool. Look at `strtok` or `strchr` instead. Too broad anyways. – Eugene Sh. Sep 11 '18 at 17:42
  • Have you tried anything yet? What exactly was the problem? – Mureinik Sep 11 '18 at 17:47
  • @EugeneSh. My assignment specifically tells me to use strstr and not to use strtok.. – renshencha Sep 11 '18 at 17:47
  • Then I would guess it does not want you to use *any* library provided string parsing functions and wants you to implement it using C primitives. – Eugene Sh. Sep 11 '18 at 17:49
  • @Mureinik Yes ,I can get first part by making two pointers using strstr and subtracting the substring, but I dont know how to make it work for the rest of the string. – renshencha Sep 11 '18 at 17:50
  • @renshencha 1) requirements like "not to use strstr and not to use strtok" belong in the question, not only in a comment. 2) Post what code you have tried. – chux - Reinstate Monica Sep 11 '18 at 17:50
  • 1
    @renshencha: Then you should post your code and tell us what is working and what isn't working. – Benjamin J. Sep 11 '18 at 17:50
  • Keep two pointers, one "previous" and the other "current". Set "previous" to the start of the string. In a loop, set "current" to the first instance of the delimiter ":" or the string's nul terminator. Use memcpy to get the bytes between "previous" and "current"-1. If "current" is at the nul terminator, break out of the loop. Else, set "previous" to the position of "current"+1, and repeat the loop. – Alex Reynolds Sep 11 '18 at 17:53
  • You can do this with a loop as @AlexReynolds suggested. But we're not here to write a complete program for you. I think you should try to implement the loop. If you get stuck you can come back and ask for specific help. – Benjamin J. Sep 11 '18 at 18:11

1 Answers1

0

find different substrings of information from a larger string where the information is separated by ":"
How could I using strstr function in C loop this string to extract the information into different substring?

strstr() isn't the best tool for this task, but it can be used to look for a ":".
Instead I'd recommend strcspn(string, "\n") as the goal is to find the next ":" or null character.

OP'c code is close to forming a loop, yet needs to also handle the last token, which lacks a final ':'.

void parse_colon(const char *string) {
  while (*string) { // loop until at the end
    size_t len;
    const char *endptr = strstr(string, ":"); // much like OP's code
    if (endptr) {
      len = (size_t) (endptr - string);  // much like OP's code
      endptr++;
    } else {
      len = strlen(string);
      endptr = string + len;
    }
    char *result = malloc(len + 1);
    if (result) {
      memcpy(result, string, len);
      result[len] = '\0';
      puts(result);
      free(result);
    }
    string = endptr;
  }
}

int main(void) {
  parse_colon("username:id:password:info");
}

Output

username
id
password
info
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256