To split a string in C language

Source: Internet
Author: User

#include <stdio.h>intMain () {Charstr1[Ten] = {0}; Charstr2[Ten] = {0}; Charstr3[Ten] = {0}; SSCANF ("1,2,3#3,4#4,5","%[^#]#%[^#]#%[^#]", str1, STR2,STR3); printf ("The split result is:\n%s\n%s\n%s\n", str1, STR2,STR3); return 0; }  /*how to ask Hovertree.com*/
//[C] string processing-splitting a string with a specified string (supports Chinese characters)//2008-11-8: Function Stringsplit (splits the string into a string array, where the No. 0 bit of the array is the number of split strings)//2008-11-10: Function stringsplit_struct (implement the function in a way that defines a new structure)/*C code is as follows*/#include<stdio.h>/*Implementation Scenario 1*/  /*how to ask Hovertree.com*//*splits the string into a string array, where the first bit of the array is the number of splits*/  Char* * Stringsplit (Const Char*string,Const Char*split) {       Char**result; /*first allocate a char* of memory, and then dynamically allocate the remaining memory*/result= (Char* * )malloc(sizeof(Char*)*1); memset (Result,0,sizeof(Char*)*1); /*define a traversal pointer and a pointer to find the position*/       Char* p =string; Char* pos =string; /*Whether or not the split string exists, it is absolutely split into a string*/       intCount =1;  while(*p! =' /')       {           Char*temp; Char*tt; /*Find the String*/POS=strstr (P,split); /*A result of 0 indicates that the remaining string does not have the character.*/           if(pos = =0) {result= (Char* * )realloc(Result,sizeof(Char*) * (count+2)); result[0] =count; Result[count]=p; Result[count+1] =NULL; returnresult; }           /*allocating temporary string space*/Temp= (Char* )malloc(sizeof(Char) * (pos-p+1)); memset (temp,0,sizeof(Char) * (pos-p+1)); /*sets the head pointer to use when assigning values*/TT=temp;  while(p<=POS) {             *temp++ = *p++; }           /*end the string by 0*/*--temp =' /'; Result= (Char* * )realloc(Result,sizeof(Char*) * (count+1)); result[0] =count; Result[count]=tt; Count++; /*sets a pointer to the next traversal (important). When split length is greater than 1 o'clock, it is not set to assign more than an unnecessary string*/P+=strlen (split)-1; }       returnresult; }  /*Implementation Scenario 2*/  /*how to ask Hovertree.com*//*to facilitate the enumeration of defined structures, the array of strings is assigned starting from 0*/typedefstruct{       intNumber/*number of separated strings*/       Char**string;/*Array of strings*/}stringtab; /*splits a string into an array of strings*/Stringtab stringsplit_struct (Const Char*string,Const Char*split)       {Stringtab result; /*first allocate a char* of memory, and then dynamically allocate the remaining memory*/result.string= (Char* * )malloc(sizeof(Char*)*1); memset (result.string,0,sizeof(Char*)*1); /*Whether or not the split string exists, it is absolutely split into a string*/Result.number=0; /*define a traversal pointer and a pointer to find the position*/       Char* p =string; Char* pos =string;  while(*p! =' /')       {          Char*temp; Char*tt; /*Find the String*/POS=strstr (P,split); /*A result of 0 indicates that the remaining string does not have the character.*/          if(pos = =0) {result.string= (Char* * )realloc(result.)string,sizeof(Char*) * (result.number+1)); Result.string[Result.number] =p; returnresult; }          /*allocating temporary string space*/Temp= (Char* )malloc(sizeof(Char) * (pos-p+1)); memset (temp,0,sizeof(Char) * (pos-p+1)); /*sets the head pointer to use when assigning values*/TT=temp;  while(p<=POS) {            *temp++ = *p++; }          /*end the string by 0*/*--temp =' /'; Result.string= (Char* * )realloc(result.)string,sizeof(Char*) * (result.number+1)); Result.string[Result.number] =tt; /*counter plus one*/Result.number++; /*sets a pointer to the next traversal (important). When split length is greater than 1 o'clock, it is not set to assign more than an unnecessary string*/P+=strlen (split)-1; }       returnresult; }    intMain () {/*to test*/ /*how to ask Hovertree.com*/      /*Scenario 1 Testing*/       Char**Array; Array= Stringsplit ("a/aaa//ha aa","AAA"); inti;  for(i=1; I<= (int) array[0];i++) {printf ("num:%d i:%d:value:%s\n", array[0],i,array[i]); } Array= Stringsplit ("a/aa ha a//ha aa","ha");  for(i=1; I<= (int) array[0];i++) {printf ("num:%d i:%d:value:%s\n", array[0],i,array[i]); }         /*Scenario 2 Testing*/Stringtab array2; Array2= Stringsplit_struct ("a/aaa//ha aa","AAA");  for(i=0; i<=array2.number;i++) {printf ("num:%d i:%d:value:%s\n", Array2.number,i,array2.string[i]); } array2= Stringsplit_struct ("a/aa ha a//ha aa","ha");  for(i=0; i<=array2.number;i++) {printf ("num:%d i:%d:value:%s\n", Array2.number,i,array2.string[i]); }         return 0; } 

Related: http://www.cnblogs.com/roucheng/p/cfenge.html

To split a string in C language

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.