Concatenation and splitting instances of c language strings
- 2020-06-23 01:29:43
- OfStack
1. String concatenation
char *strcat(char *str_des, char *str_sou);
Place the string str_sou after the string str_des (between the last character of str_des and "\0").
Be careful not to cross the line. Use the strlen(input) function to find the length of the string before stitching it.
2. String segmentation
char *strtok(char *str_sou,constchar *str_sep);
str_sou: String to be split. str_sep: Segmentation symbol.
First call: temp = strtok(input, a); (input: string, a: separator);
Then call: 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];
// Joining together, a : Segmentation symbol; b,c : 2 A string
char *a = ":", *b = "1", *c = " I am a qy";
printf(" String before splicing (garbled code) :%s\n",input); //input It's not initialized, it's a garbled print
strcpy(input,b);
strcat(input,a);
strcat(input,c);
printf(" The spliced string :%s\n",input);
// Length: printf(" The length of the spliced string : %d\n",strlen(input));
char *temp;
temp = strtok(input, a);
if (temp)
printf(" The string before the split symbol : %s\n", temp);
temp = strtok(NULL, a);
if (temp)
printf(" The string after the split symbol : %s\n",temp);
return 0;
}