Use strtok to separate sentences

Source: Internet
Author: User
Tags strtok

Char * strtok (char * string, const char * delimiters );

Sequentially truncate string if Delimiter is found.
If string is notNull, The function scans string for the first occurrence of any character defined ded in delimiters. if it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, I. e. the part of the scanned string previous to the delimiter.
After a first callStrtok, The function may be calledNullAs string parameter, and it will follow by where the last callStrtokFound a delimiter.
Delimiters may vary from a call to another.

Parameters.

String
Null-terminated string to scan.
Separator
Null-terminated string containing the separators.

Return Value.
A pointer to the last token found in string.NullIs returned when there are no more tokens to be found.

Portability.
Defined in ANSI-C.

Example.

/* Strtok example */# include <stdio. h> # include <string. h> int main () {char STR [] = "This is a sample string, just testing. "; char * PCH; printf (" splitting string \ "% s \" in tokens: \ n ", STR); PCH = strtok (STR ,""); while (PCH! = NULL) {printf ("% s \ n", PCH); PCH = strtok (null, ",.");} return 0 ;}

Output:
Splitting string "This is a sample string, just testing." In tokens:
This
Is
A
Sample
String
Just
Testing

The following is strtok manual in Linux:
---------------------------------------------------------------
Strtok (3) Linux programmer's Manual strtok (3)

 

Name
Strtok, strtok_r-extract tokens from strings

Synopsis
# Include <string. h>

Char * strtok (char * s, const char * delim );

Char * strtok_r (char * s, const char * delim, char ** ptrptr );

description
A 'Token' is a nonempty string of characters not occurring in the
string delim, followed by \ 0 or by a character occurring in delim.

the strtok () function can be used to parse the string s into tokens.
the first call to strtok () shocould have s as its first argument. subse-
quent cballs shoshould have the first argument set to null. each call
Returns a pointer to the next token, or null when no more tokens are
found.

If a token ends with a delimiter, this delimiting character is over-
written with a \ 0 and a pointer to the next character is saved for the
next call to strtok (). the delimiter string delim may be different for
each call.

The strtok_r () function is a reentrant version of the strtok () func-
Tion, which instead of using its own static buffer, requires a pointer
To a user allocated char *. This pointer, The ptrptr parameter, must be
The same while parsing the same string.

Bugs
Never use these functions. If you do, note that:

These functions modify their first argument.

These functions cannot be used on constant strings.

The identity of the delimiting character is lost.

The strtok () function uses a static buffer while parsing, so
It's not thread safe. Use strtok_r () If this matters to you.

Return Value
The strtok () function returns a pointer to the next token, or null if
There are no more tokens.

Conforming
Strtok ()
Svid 3, POSIX, BSD 4.3, ISO 9899

Strtok_r ()
Posix.1c

See also
Index (3), memchr (3), rindex (3), strchr (3), strpbrk (3), strsep (3), str-
SPNs (3), strstr (3)

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.