The use of the C-language string command Strstr () enables the original string to be split into a split-string output "go"

Source: Internet
Author: User

The strstr () command is to find the address of the first occurrence of the specified string in the original string, which enables the segmentation of the character and the ability to determine if the inclusion is possible:

The following example is an implementation string that splits the output of a split string:

 1 #include <stdio.h> 
2 #include <string.h>
3
4 int main ()
5 {
6 char *str= "aaa| |a| | bbb| | c| | ee| | ";
7 char *sp= "| |";
8
9 Char *pos = STRSTR (STR,SP);//Find the address of the delimiter from the original string
Ten char *lastpos = str;//Last address, first of course the original string's first address
11 while (pos! = NULL)
Ten {
(Pos-lastpos) > 0)//Two split strings exist between characters
+
15 int i = 0;
Do
+ {
printf ("%c", * (Lastpos + i));//output characters from the previous address to the current address
19 i++;
20}
while ((Lastpos + i) < POS);
printf ("\ n");//Add line wrapping at the end
+
Lastpos = pos + strlen (SP);//new String header is the address location that was last found + Split-length
pos = strstr (LASTPOS,SP);//start looking for the next delimiter position from the new string address
"
" return 0;
*/*
29 Output:
+ AAA
+ a
+ BBB
+ C
ee
*/
3 6}

The use of the C-language string command Strstr () enables the original string to be split into a split-string output "go"

Related Article

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.