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