concatenation and segmentation of C-language strings __c language

Source: Internet
Author: User
Tags strtok

1. Concatenation of strings

Functions using C char *strcat (char *str_des, char *str_sou);

Str_sou the string after the string str_des (placed between the last character of the Str_des and "the").

Be careful not to cross over, use strlen (input) function to find string length and then stitching.

2. Segmentation of strings

Use C's function char *strtok (char *str_sou,constchar *str_sep);

Str_sou: The string to be split. STR_SEP: Split symbol.

First Call: temp = strtok (input, a); (Input: String, a: delimiter);

Called after: temp = Strtok (NULL, a);

Temp is the string to be split.

3. Demo

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

int main (void)
{
Char input[16];
Stitching, A: dividing a symbol; B,c:2 a string
Char *a = ":", *b = "1", *c = "I am QY";
printf ("stitching before the string (garbled):%s\n", input); Input is not initialized, the print is garbled

strcpy (INPUT,B);
strcat (Input,a);
strcat (INPUT,C);
printf ("Concatenation of Strings:%s\n", input);

Length: printf ("The length of the string after concatenation:%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 split symbol:%s\n", temp);
return 0;
}

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.