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
1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

strtok example

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.