原文: https://beginnersbook.com/2014/01/c-strings-string-functions/
字符串是一个字符数组。在本指南中,我们将学习如何声明字符串,如何在 C 编程中使用字符串以及如何使用预定义的字符串处理函数。
我们将看到如何比较两个字符串,连接字符串,将一个字符串复制到另一个字符串,以及执行各种字符串操作。我们可以使用string.h头文件的预定义函数执行此类操作。要使用这些字符串函数,必须在 C 程序中包含string.h文件。
字符串声明

方法 1:
char address[]={'T', 'E', 'X', 'A', 'S', '\0'};
方法 2:上面的字符串也可以定义为:
char address[]="TEXAS";
在上面的声明中,空字符(\0)将自动插入字符串的末尾。
什么是空字符\0?
'\0'表示字符串的结尾。它也被称为字符串结尾或空字符。
C 编程中的字符串 I/O

使用printf()和scanf()函数在 C 中读取和编写字符串
#include <stdio.h>#include <string.h>int main(){/* String Declaration*/char nickname[20];printf("Enter your Nick name:");/* I am reading the input string and storing it in nickname* Array name alone works as a base address of array so* we can use nickname instead of &nickname here*/scanf("%s", nickname);/*Displaying String*/printf("%s",nickname);return 0;}
输出:
Enter your Nick name:NeganNegan
注意:%s格式说明符用于字符串输入/输出
使用gets()和puts()函数在 C 中读取和编写字符串
#include <stdio.h>#include <string.h>int main(){/* String Declaration*/char nickname[20];/* Console display using puts */puts("Enter your Nick name:");/*Input using gets*/gets(nickname);puts(nickname);return 0;}
C - 字符串函数

C 字符串函数 - strlen
语法:
size_t strlen(const char *str)
size_t 表示无符号短整数,它返回字符串的长度而不包括结束字符(终止字符\0)。
strlen的例子:
#include <stdio.h>#include <string.h>int main(){char str1[20] = "BeginnersBook";printf("Length of string str1: %d", strlen(str1));return 0;}
输出:
Length of string str1: 13
strlen vs sizeof
strlen返回存储在数组中的字符串的长度,但sizeof返回分配给数组的总大小。因此,如果我再次考虑上述示例,则以下语句将返回以下值。
strlen(str1)返回值 13。sizeof(str1)将返回值 20,因为数组大小为 20(请参阅main函数中的第一个语句)。
C 字符串函数 - strnlen
语法:
size_t strnlen(const char *str, size_t maxlen)
size_t表示无符号短整数。如果字符串小于为maxlen指定的值(最大长度),则返回字符串的长度,否则返回maxlen值。
strnlen的例子:
#include <stdio.h>#include <string.h>int main(){char str1[20] = "BeginnersBook";printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));return 0;}
输出:
Length of string str1 when maxlen is 30: 13Length of string str1 when maxlen is 10: 10
您是否注意到第二个printf语句的输出,即使字符串长度为 13,它只返回 10,因为maxlen为 10。
C 字符串函数 - strcmp
int strcmp(const char *str1, const char *str2)
它比较两个字符串并返回一个整数值。如果两个字符串相同(相等),则此函数将返回 0,否则它可能会根据比较返回负值或正值。
如果string1<string2或者string1是string2的子字符串,它会产生负值。如果string1>string2它将返回正值。
如果string1 == string2 ,那么当你将此函数用于比较字符串时,你会得到 0。
strcmp示例:
#include <stdio.h>#include <string.h>int main(){char s1[20] = "BeginnersBook";char s2[20] = "BeginnersBook.COM";if (strcmp(s1, s2) ==0){printf("string 1 and string 2 are equal");}else{printf("string 1 and 2 are different");}return 0;}
输出:
string 1 and 2 are different
C 字符串函数 - strncmp
int strncmp(const char *str1, const char *str2, size_t n)
size_t用于无符号短整数。它比较字符串直到n个字符,或者换句话说,它比较两个字符串的前n个字符。
strncmp示例:
#include <stdio.h>#include <string.h>int main(){char s1[20] = "BeginnersBook";char s2[20] = "BeginnersBook.COM";/* below it is comparing first 8 characters of s1 and s2*/if (strncmp(s1, s2, 8) ==0){printf("string 1 and string 2 are equal");}else{printf("string 1 and 2 are different");}return 0;}
输出:
string1 and string 2 are equal
C 字符串函数 - strcat
char *strcat(char *str1, char *str2)
它连接两个字符串并返回连接的字符串。
strcat示例:
#include <stdio.h>#include <string.h>int main(){char s1[10] = "Hello";char s2[10] = "World";strcat(s1,s2);printf("Output string after concatenation: %s", s1);return 0;}
输出:
Output string after concatenation: HelloWorld
C 字符串函数 - strncat
char *strncat(char *str1, char *str2, int n)
它将str2的n个字符连接到字符串str1。终结符(\0)将始终附加在连接字符串的末尾。
strncat示例:
#include <stdio.h>#include <string.h>int main(){char s1[10] = "Hello";char s2[10] = "World";strncat(s1,s2, 3);printf("Concatenation using strncat: %s", s1);return 0;}
输出:
Concatenation using strncat: HelloWor
C 字符串函数 - strcpy
char *strcpy( char *str1, char *str2)
它将字符串str2复制到字符串str1中,包括结束字符(终结符\0)。
strcpy示例:
#include <stdio.h>#include <string.h>int main(){char s1[30] = "string 1";char s2[30] = "string 2 : I’m gonna copied into s1";/* this function has copied s2 into s1*/strcpy(s1,s2);printf("String s1 is: %s", s1);return 0;}
输出:
String s1 is: string 2: I’m gonna copied into s1
C 字符串函数 - strncpy
char *strncpy(char *str1, char *str2, size_t n)
size_t是无符号short,n是数字。
情况 1:如果str2的长度>n然后它只是将str2的前n个字符复制到str1中。
情况 2:如果str2的长度<n。然后它将str2的所有字符复制到str1中,并附加几个终结符字符(\0)以填充str1的长度使其成为n。
strncpy的例子:
#include <stdio.h>#include <string.h>int main(){char first[30] = "string 1";char second[30] = "string 2: I’m using strncpy now";/* this function has copied first 10 chars of s2 into s1*/strncpy(s1,s2, 12);printf("String s1 is: %s", s1);return 0;}
输出:
String s1 is: string 2: I’m
C 字符串函数 - strchr
char *strchr(char *str, int ch)
它在字符串str中搜索字符ch(您可能想知道在上面的定义中我已经将ch的数据类型赋予了int,不要担心我没有犯任何错误它应该只是int。事情是当我们将任何值给strchr时,它会在内部转换为整数以便更好地搜索。
strchr的例子:
#include <stdio.h>#include <string.h>int main(){char mystr[30] = "I’m an example of function strchr";printf ("%s", strchr(mystr, 'f'));return 0;}
输出:
f function strchr
C 字符串函数 - strrchr
char *strrchr(char *str, int ch)
它类似于strchr函数,唯一的区别是它以相反的顺序搜索字符串,现在你已经理解为什么我们在strrchr中有额外的r,是的你猜对了它,它只是反向的。
现在让我们采用相同的上述示例:
#include <stdio.h>#include <string.h>int main(){char mystr[30] = "I’m an example of function strchr";printf ("%s", strrchr(mystr, 'f'));return 0;}
输出:
function strchr
为什么输出与strchr不同? 这是因为它从字符串的末尾开始搜索并在函数中找到第一个'f'而不是'of'。
C 字符串函数 - strstr
char *strstr(char *str, char *srch_term)
它类似于strchr,除了它搜索字符串srch_term而不是单个字符。
strstr示例:
#include <stdio.h>#include <string.h>int main(){char inputstr[70] = "String Function in C at BeginnersBook.COM";printf ("Output string is: %s", strstr(inputstr, 'Begi'));return 0;}
输出:
Output string is: BeginnersBook.COM
您也可以使用此函数代替strchr,因为您也可以使用单个字符代替search_term字符串。
