The common string manipulation of the Azusa C language Learning Note (SSCANF & strtok)

Source: Internet
Author: User
Tags strtok

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.