Examples of C language string splicing and segmentation

Time:2022-5-3

1. String splicing

Use the function char * strcat of C (char * str_des, char * str_sou);

String str_ Sou followed by STR_ Des (placed between the last character of str_des and “\ 0”).

Be careful not to cross the line. You can use the strlen (input) function to find the length of the string before splicing.

2. String segmentation

Use the function char * strtok of C (char * str_sou, constchar * str_sep);

str_ Sou: string to be split. str_ SEP: split symbol.

First call: temp = strtok (input, a); (input: string, a: separator);

Call later: temp = strtok (null, a);

Temp is the string obtained after segmentation.

3. demo

#include <string.h>
#include <stdio.h>

int main(void)
{
  char input[16];
//Splicing, a: Division symbol; b. C: 2 strings
Char * a = ":", * b = "1", * C = "I'm QY";
Printf ("string before splicing (garbled):% s \ n", input)// The input is not initialized, and the printed code is garbled

strcpy(input,b);
strcat(input,a);
strcat(input,c);
Printf ("spliced string:% s \ n", input);

//Length: printf ("length of spliced string:% d \ n", strlen (input));
  char *temp;
  temp = strtok(input, a);
  if (temp) 
Printf ("string before split symbol:% s \ n", temp);
temp = strtok(NULL, a);
  if (temp) 
Printf ("string after splitting symbol:% s \ n", temp);
  return 0;
}

The above example of C language string splicing and segmentation is all the content shared by Xiaobian. I hope it can give you a reference and support developeppaer.

Recommended Today

Big data Hadoop — spark SQL + spark streaming

catalogue 1、 Spark SQL overview 2、 Sparksql version 1) Evolution of sparksql 2) Comparison between shark and sparksql 3)SparkSession 3、 RDD, dataframes and dataset 1) Relationship between the three 1)RDD 1. Core concept 2. RDD simple operation 3、RDD API 1)Transformation 2)Action 4. Actual operation 2)DataFrames 1. DSL style syntax operation 1) Dataframe creation 2. SQL […]