String partitioning function and its implementation method in C language _c language

Source: Internet
Author: User
Tags strtok

1. Introduction of the problem

When I wrote a simulated execution instruction under Linux, I encountered the input "cat a.c", To break the string down into two separate strings for cat and A.C, although I know there is a strtok, but I want to try to write, so I wrote one, but always encountered such or such a problem, although the final adjustment, but indeed wasted a lot of time; later, after handing in the homework and carefully read the Strtok function, found The original Linux has been changed to STRSEP, all here to write the process of their walk.

2, write their own string segmentation function: For segmentation instructions, such as Cat A.C will eventually be divided into cat and a.c two strings, MV A.C B.C will eventually be divided into MV and A.C and B.C three strings.

Specifically implemented as follows:

#include <stdio.h> #include <string.h> #define Max_len 128 void Main () {int i,length,ct=0,start =-1;
  Char Inputbuffer[max_len],*args[max_len];
  strcpy (InputBuffer, "MV A.C B.C");
   Length=strlen (InputBuffer); for (i = 0; I <= length; i++) {switch (Inputbuffer[i]) {case ": Case ' t ':/* Argument separator  S */if (start!=-1) {ARGS[CT] = &inputBuffer[start];
      /* Set up pointer * * ct++; } Inputbuffer[i] = '; /* Add a null char;
      Make a C String */start =-1;
    Break Case ' I ':/* should be the final char examined */if (start!=-1) {ARGS[CT] = &inputbuffer[sta
        RT];
      ct++;
      } Inputbuffer[i] = '; ARGS[CT] = NULL;
    * No more arguments to the this command * * break;
    Default:/* Some other character */if (start = = 1) start = i;
  printf ("The string after decomposition is: \ n"); for (i=0;i<ct;i++) printf ("%s \ n", Args[i]); }

3, the job submitted and query the Strtok, found that the use of strtok function will be more convenient

The specific examples are as follows:

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] = "mv a.c B.C";
  char *p; 
  p = strtok (str, "");
  while (p)
  {  
    printf ("%s\n", p);  
    p = strtok (NULL, "");  
  }
  return 0;
}

4, in the later version of Linux2.6.29, Strtok was replaced by STRSEP.

The specific examples are as follows:

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] = "mv a.c B.C";
  char *p;
  char *buff;
  Buff=str;
  p = strsep (&buff, "");
  while (p)
  {
    printf ("%s\n", p);
    p = strsep (&buff, "");
  }
  return 0;
}

And in the Linux codeblog of my own computer, the code in run 4 requires 0.029s, and the code running 3 requires 0.044s, which means that strsep speed is indeed faster than strtok.

The above C language string segmentation function and implementation is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.