parsing incoming strings

If my following line input is "Home: test me"

I am trying to write a sscanf to skip everything up to a specific character in this case a ':' and the final out put would be p = "test" t = "me"

also suppose the incoming line did not even contain a ':' it still should read in the line and parse it. for example the incoming string "Home test me" should now be p = "Home" t = "test"

1
2
3
4
5
6
7
8
  int main(){
        char p[10],t[100];
          
        if( sscanf(": %s %s", p, t) == 2);
         printf("%s", p, t);
        else
          { -do something- }
    }
Last edited on
Do you have to do it with sscanf?
If you really want to use the old C....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
  char p[10], t[100], input[] = "Home: test me";
  char *pos = strchr(input, ':');
  if (pos != 0)
  {
    if (sscanf(++pos, "%s %s", p, t) == 2)
    {
      printf("\np = %s", p);
      printf("\nt = %s", t);
    }
  }
  return 0;
}
Topic archived. No new replies allowed.