{"title":"C语言字符串函数大全:原型、功能及示例 - 提升代码效率","description":"本文全面介绍C语言中常用的字符串函数,包括strlen、strcpy、strcat、strcmp等,并提供详细的函数原型、功能说明及示例代码,帮助您快速掌握字符串操作技巧,提高代码效率。","keywords":"C语言, 字符串函数, strlen, strcpy, strcat, strcmp, strncpy, strncat, strchr, strrchr, strstr, 字符串操作, 代码效率","content":"## C语言字符串函数大全:原型、功能及示例

在C语言中,字符串操作是常见的编程任务。为了简化字符串操作,C标准库提供了丰富的字符串函数,本文将详细介绍这些函数的原型、功能及示例。

1. strlen(const char *str)

原型: size_t strlen(const char *str);

功能: 返回字符串的长度(不包括空字符'\0')。

示例:

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

int main() {
  char str[] = "Hello World!";
  size_t len = strlen(str);
  printf("字符串长度:%zu\n", len);
  return 0;
}

2. strcpy(char *destination, const char *source)

原型: char *strcpy(char *destination, const char *source);

功能: 将源字符串复制到目标字符串中。

示例:

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

int main() {
  char dest[50];
  char source[] = "Hello World!";
  strcpy(dest, source);
  printf("目标字符串:%s\n", dest);
  return 0;
}

3. strncpy(char *destination, const char *source, size_t n)

原型: char *strncpy(char *destination, const char *source, size_t n);

功能: 将源字符串的前n个字符复制到目标字符串中。

示例:

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

int main() {
  char dest[50];
  char source[] = "Hello World!";
  strncpy(dest, source, 5);
  printf("目标字符串:%s\n", dest);
  return 0;
}

4. strcat(char *destination, const char *source)

原型: char *strcat(char *destination, const char *source);

功能: 将源字符串追加到目标字符串的末尾。

示例:

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

int main() {
  char dest[50] = "Hello";
  char source[] = " World!";
  strcat(dest, source);
  printf("目标字符串:%s\n", dest);
  return 0;
}

5. strncat(char *destination, const char *source, size_t n)

原型: char *strncat(char *destination, const char *source, size_t n);

功能: 将源字符串的前n个字符追加到目标字符串的末尾。

示例:

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

int main() {
  char dest[50] = "Hello";
  char source[] = " World!";
  strncat(dest, source, 5);
  printf("目标字符串:%s\n", dest);
  return 0;
}

6. strcmp(const char *str1, const char *str2)

原型: int strcmp(const char *str1, const char *str2);

功能: 比较两个字符串,并返回一个整数表示它们的相对顺序。

  • 如果str1小于str2,则返回一个负数。
  • 如果str1等于str2,则返回0。
  • 如果str1大于str2,则返回一个正数。

示例:

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

int main() {
  char str1[] = "abc";
  char str2[] = "def";
  int result = strcmp(str1, str2);
  printf("比较结果:%d\n", result);
  return 0;
}

7. strncmp(const char *str1, const char *str2, size_t n)

原型: int strncmp(const char *str1, const char *str2, size_t n);

功能: 比较两个字符串的前n个字符,并返回一个整数表示它们的相对顺序。

  • 如果str1小于str2,则返回一个负数。
  • 如果str1等于str2,则返回0。
  • 如果str1大于str2,则返回一个正数。

示例:

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

int main() {
  char str1[] = "abc";
  char str2[] = "abd";
  int result = strncmp(str1, str2, 2);
  printf("比较结果:%d\n", result);
  return 0;
}

8. strchr(const char *str, int c)

原型: char *strchr(const char *str, int c);

功能: 在字符串中搜索指定字符,并返回指向该字符的指针。

示例:

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

int main() {
  char str[] = "Hello World!";
  char *ptr = strchr(str, 'o');
  if (ptr != NULL) {
    printf("字符 'o' 在字符串中第一次出现的位置:%s\n", ptr);
  } else {
    printf("字符 'o' 不存在于字符串中。\n");
  }
  return 0;
}

9. strrchr(const char *str, int c)

原型: char *strrchr(const char *str, int c);

功能: 在字符串中搜索指定字符的最后一次出现,并返回指向该字符的指针。

示例:

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

int main() {
  char str[] = "Hello World!";
  char *ptr = strrchr(str, 'o');
  if (ptr != NULL) {
    printf("字符 'o' 在字符串中最后一次出现的位置:%s\n", ptr);
  } else {
    printf("字符 'o' 不存在于字符串中。\n");
  }
  return 0;
}

10. strstr(const char *haystack, const char *needle)

原型: char *strstr(const char *haystack, const char *needle);

功能: 在字符串中搜索指定的子字符串,并返回指向该子字符串的指针。

示例:

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

int main() {
  char haystack[] = "Hello World!";
  char needle[] = "World";
  char *ptr = strstr(haystack, needle);
  if (ptr != NULL) {
    printf("子字符串 'World' 在字符串中第一次出现的位置:%s\n", ptr);
  } else {
    printf("子字符串 'World' 不存在于字符串中。\n");
  }
  return 0;
}

总结

本文介绍了C语言中常用的字符串函数,希望能够帮助您更好地理解和使用这些函数,提高代码效率。在实际编程中,可以根据具体需求选择合适的函数进行操作。

C语言字符串函数大全:原型、功能及示例 - 提升代码效率

原文地址: https://www.cveoy.top/t/topic/pSmW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录