Home > Backend Development > C++ > body text

Usage of string function in c language

下次还敢
Release: 2024-04-29 20:30:26
Original
766 people have browsed it

string The function library provides functions for operating strings, including: string comparison functions (strcmp(), strncmp(), strcasecmp()) string copy functions (strcpy(), strncpy()) characters String connection functions (strcat(), strncat()) String search functions (strchr(), strstr()) String conversion functions (strtol(), strtof(), strcpy()) String formatting functions (sprintf() ), sscanf())

Usage of string function in c language

Usage of string function in C language

Question: What is the use of string function in C language?

Answer: The string function library provides functions for operating strings, including string comparison, copying, concatenation, search, conversion and formatting, etc.

Detailed description:

String comparison function:

  • strcmp(): Compare two strings Equality
  • strncmp(): Compares the first n characters of two strings to see if they are equal
  • strcasecmp(): Compares two strings case-insensitively

String copy function:

  • strcpy(): Copy one string to another string
  • strncpy(): Copy a character Copy the first n characters of the string to another string

String concatenation function:

  • strcat(): Append a string To another string
  • strncat(): Append the first n characters of a string to another string

String search function:

  • strchr(): Find the first specified character in the string
  • strstr(): Find the first specified substring in the string

String conversion function:

    ##strtol(): Convert a string to a long integer
  • strtof(): Convert a string to float Floating point number
  • strcpy(): Convert a string to uppercase or lowercase
##String formatting function:

sprintf(): Format data into a string
  • sscanf(): Extract data from a string
Example:

<code class="c">#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";

    // 比较字符串
    int result = strcmp(str1, str2);
    if (result == 0) {
        printf("字符串相等\n");
    } else if (result < 0) {
        printf("str1 小于 str2\n");
    } else {
        printf("str1 大于 str2\n");
    }

    // 复制字符串
    strcpy(str1, str2);
    printf("str1 现在是 %s\n", str1);

    // 连接字符串
    strcat(str1, "C");
    printf("str1 现在是 %s\n", str1);

    // 搜索字符串
    char *pos = strchr(str1, 'o');
    if (pos != NULL) {
        printf("字符 'o' 在字符串中\n");
    }

    // 转换字符串
    int num = strtol(str2, NULL, 10);
    printf("str2 转换为整数为 %d\n", num);

    return 0;
}</code>
Copy after login

The above is the detailed content of Usage of string function in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!