The common string manipulation of the Azusa C language Learning Note (SSCANF & strtok)
First, sscanf
int sscanf (const char *buf,const char *format, ...); \ \ Reads information from the memory area specified by BUF
Examples:int A, b, C;
SSCANF ("2013:10:1", "%d:%d:%d", &a, &b, &c);
printf ("%d%d%d\n", a,b,c);
The regular expression for sscanf () is to find the matching character from the back-up order and return an error if it is not found, as in the following example:
Char src[] = "[email protected]";//a BUF only corresponds to one%,%* can be used multiple times
SSCANF (SRC, "%[a-z]", buf),//buf= null, encounters a A-Z character on the stop,% for selection,% for the representation condition, and the condition s to represent any character
SSCANF (SRC, "%[^a-z]", buf);//buf=abcdef,^ means reverse, that is, a character between A-Z is ended
SSCANF (SRC, "%[^a]", buf);//buf=abcdef, gets characters except a from left to right until a results
SSCANF (SRC, "%*[^a]", buf);//buf= NULL, * indicates that the character after the match is not saved ([^a] should be abcdef, but because of the addition of * so there is no filter out)
SSCANF (SRC, "%*[^a]%s", buf);//[email protected],s represents getting a string, although the character is not previously saved, but subsequent characters can be obtained and saved
SSCANF (SRC, "%*[^a]%[^@]", buf);//buf=abcd123 until you encounter a and include a end
SSCANF (SRC, "%*[^a]a%[^@]", buf),//buf=bcd123, until a is encountered but does not contain a end, A is a delimiter
Second, strtok
Char *strtok (char *str, const char *delim);
#include <stdio.h>
#include <string.h>
int main ()
{
Char str1[]= "Adc*fvcv*!ebcy!hghbdfg*casdert";
Char *str2= "*!";
Char *result[10];
int num=0,i;
Result[num]=strtok (STR1,STR2);
while (Result[num]!=null)
{
num++;
Result[num]=strtok (NULL,STR2);
}
for (i=0;i<num;i++)
printf ("result[%d]=%s\n", I,result[i]);
return 0;
}
The common string manipulation of the Azusa C language Learning Note (SSCANF & strtok)