A detailed explanation of string processing function in C language

Source: Internet
Author: User
Tags strcmp strlen

C language provides rich string processing functions, such as string input, output, merge, modify, compare, transform, copy, search, etc., use these ready-made functions can greatly reduce the burden of programming.

String functions for input and output, such as printf, puts, scanf, gets, and so on, should include header file stdio.h when used, and other string functions should include header file string.h.

1. String length function strlen

The strlen is the abbreviation for string length, which is used to get the length of strings. The so-called length is the number of characters (excluding the end of the string). The syntax format is:

Strlen (Arrayname);
Strlen will return the length of the string, which is an integer. Take a look at the following example:

#include <stdio.h>
#include <string.h>
int main () {
Char str[]= "C language";
int len = strlen (str);
printf ("The lenth of the string is%d\n", Len);
return 0;
}
Run Result:

The lenth of the string is 10
It should be explained that strlen will start with the No. 0 character of the string until it encounters the string end flag '. Change the str in the above code to:

Char str[]= "C \0language";
So the output is:

The lenth of the string is 2

2. String concatenation function strcat

STRCAT is the abbreviation for string catenate, which means that two strings are spliced together in a syntax format:

strcat (arrayName1, arrayName2);
ArrayName1 and arrayName2 are strings that need stitching.

The strcat will connect the arrayName2 to the arrayName1 and delete the arrayName1 end sign '.

This means that the length of the arrayName1 is sufficient and must be able to accommodate both arrayName1 and arrayName2, otherwise it will go out of bounds.

The strcat return value is the first address of arrayName1. Take a look at the following example:

#include <stdio.h>
#include <string.h>
int main () {
Char str1[40]= "My name is";
Char str2[20];
printf ("Input Your Name:");
Gets (STR2);
strcat (STR1,STR2);
Puts (STR1);
return 0;
}
Run Result:

Input your Name:xuhao
My name is Xuhao
3. String copy function strcpy

strcpy is a string copy of the abbreviation, meaning that the strings are copied, and the syntax format is:

strcpy (arrayName1, arrayName2);
The strcpy will copy the strings in the arrayName2 to the arrayName1, and the end of the string is also copied together. Take a look at the following example:

#include <stdio.h>
#include <string.h>
int main () {
Char str1[15], str2[]= "C Language";
strcpy (str1, str2);
Puts (STR1);
printf ("\ n");
return 0;
}
Run Result:

C Language

Strcat requires the arrayName1 to have sufficient length, otherwise the copied string cannot be loaded.

4. String comparison function strcmp

STRCMP is the abbreviation of string compare, meaning string comparisons, with the syntax format:

strcmp (arrayName1, arrayName2);
ArrayName1 and ArrayName2 are two strings that need to be compared.

The character itself has no size, and the strcmp () is compared to the corresponding number of characters on the ASCII code table. strcmp () First the ASCII code value of the No. 0 character in the arrayName1 minus the ASCII value of the No. 0 character in the arrayName2, if the difference is 0, the two characters are the same, then the next character is compared, and the difference is returned if the difference is not 0. For example, the string "Ac" and "ba" Comparisons return the Difference (-33) of the character "A" (65) and ' B ' (98).

Return value: Returns 0 if the arrayName1 and arrayName2 are the same, returns a value greater than 0 if the arrayName1 is greater than arrayName2, and returns a value less than 0 if the arrayName1 is less than arrayName2.

The following 4 sets of strings are compared.

#include <string.h>
Main () {
Char *a = "AABBCC";
Char *b = "AbCdEf";
Char *c = "Aacdef";
Char *d = "AABBCC";
printf ("strcmp (A, B):%d\n", strcmp (A, b));
printf ("strcmp (A, c):%d\n", strcmp (A, c));
printf ("strcmp (A, D):%d\n", strcmp (A, d));


GetChar ();
}
Run Result:

strcmp (A, B):-1
strcmp (A, C):-1
STRCMP (A, D): 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.