0

Here i have one character arrray in which there are some splitters are there.

character array is char sentence []="abc_12.22.32.42";

I want to read abc,12,22,32,42 from the sentence using sscanf.

I tried with sscanf but its given different output.

I posted here two code one of its working fine.

if i read abc = 12 with %s = %s then its works. Why we have to give space between splitter.

Any way to achieve this goal.

Not working Code :

#include <stdio.h>

int main ()
{
  char sentence []="abc_12.22.32.42";
  char str [20];
  char str1 [20];
  char str2 [20];
  char str3 [20];
  sscanf (sentence,"%s_%s.%s.%s.%s",str,str1,str2,str3);
  printf ("%s --> %s --> %s --> %s --> %s\n",str,str1,str2,str3);
  return 0;
}

Output :

abc_12.22.32.42-->  -- � -->�Tl�s --> (null)

Expected Output :

 abc --> 12 --> 22 --> 32 --> 42

working Code :

/* sscanf example */
#include <stdio.h>

int main ()
{
  char sentence []="abc = 12";
  char str [20];
  char str1 [20];
  char str2 [20];
  char str3 [20];
  if(sscanf(sentence,"%s = %s",str,str1) < 5) ;
        printf ("%s--> %s\n",str,str1);
  return 0;
}

output :

abc--> 12
LihO
  • 41,190
  • 11
  • 99
  • 167
user1089679
  • 2,328
  • 8
  • 41
  • 51
  • possible duplicate of [Split string with delimiters in C](http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c) – dirkgently Jun 07 '12 at 07:07

3 Answers3

3

When specifying format like this: %s_%s.%s.%s.%s, your program can't know that first string can't contain characters '_' and '.'. That's the reason why sscanf(sentence,"%s_%s.%s.%s.%s", str0, str1, str2, str3, str4); causes whole sentence to be stored in str0.

If you want to stay with sscanf, you'll have to change the format that you expect:

char sentence[] = "abc_12.22.32.42";
char str0[20], str1[20], str2[20], str3[20], str4[20];
sscanf (sentence,"%19[^_]_%19[^.].%19[^.].%19[^.].%19[^.]",
        str0, str1, str2, str3, str4);
printf ("%s --> %s --> %s --> %s --> %s\n",
        str0, str1, str2, str3, str4);

%19[^_] causes str0 to contain max 19 characters (20 with '\0') and prevents character '_' to become part of str0. Thus the output becomes abc --> 12 --> 22 --> 32 --> 42 as desired.

Alternatively you could use strtok.

LihO
  • 41,190
  • 11
  • 99
  • 167
1

use strtok function. it's the best way to split strings

strtok example

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

Replace ',' and '-' by ' ', also, you need one more array str4

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

int main ()
{
    char sentence []="abc_12.22.32.42";
    char str [20];
    char str1 [20];
    char str2 [20];
    char str3 [20];
    char str4 [20];

    int i;
    for (i=0; i<strlen(sentence); i++) {
        if (sentence[i] == '_') sentence[i] = ' ';
        if (sentence[i] == '.') sentence[i] = ' ';
    }

    sscanf (sentence,"%s %s %s %s %s",str,str1,str2,str3, str4);
    printf ("%s--> %s --> %s -->%s --> %s\n",str,str1,str2,str3, str4);
    return 0;
}
waitingkuo
  • 89,478
  • 28
  • 112
  • 118