strlen
原型
在 C 标准库中,strlen
函数的原型定义在 <string.h>
头文件中,如下所示:
size_t strlen(const char *str);
这里的 size_t
是一个无符号整数类型,它的定义通常在 <stddef.h>
头文件中。strlen
函数的参数 str
是一个指向字符数组的指针,该数组包含一个或多个字符。函数返回的是从 str
指向的字符串的开始位置到第一个空字符('\0'
)之间的字符数。
使用方法
strlen
函数通常用于获取字符串的长度以便进行字符串操作或比较。下面是一个简单的使用示例:
#include <stdio.h>
#include <string.h>
int main()
{
const char *str = "Hello, world!";
size_t length = strlen(str);
printf("The length of the string is: %zu\n", length);
return 0;
}
在这个例子中,strlen
函数计算了字符串 "Hello, world!"
的长度,并将其存储在变量 length
中,然后输出这个长度。
模拟实现
strlen
函数的模拟实现通常基于一个循环,遍历字符串中的每个字符,直到遇到空字符为止。下面是一个简单的 strlen
函数实现:
#include <stdio.h>
size_t my_strlen(const char *str)
{
size_t length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
}
int main()
{
const char *str = "Hello, world!";
size_t length = my_strlen(str);
printf("The length of the string is: %zu\n", length);
return 0;
}
在这个实现中,我们初始化一个 length
变量为 0,然后使用一个 while
循环来遍历字符串。循环继续直到遇到空字符('\0'
),此时循环终止,并返回 length
变量的值,即字符串的长度。
请注意,这个模拟实现没有处理空字符串的情况,即空字符串(""
)会返回 0。在实际的 strlen
函数中,空字符串也会返回 0。
strcpy和 strncpy
strcpy 函数
strcpy
函数用于将源字符串复制到目标字符串中,其原型如下:
char *strcpy(char *dest, const char *source);
参数 dest
是指向目标字符串的指针,source
是指向源字符串的指针。strcpy
函数会将 source
指向的字符串复制到 dest
指向的字符串中,直到遇到空字符('\0'
)为止。函数返回 dest
指针。
使用方法示例:
#include <stdio.h>
#include <string.h>
int main()
{
char dest[20];
const char *source = "Hello, world!";
strcpy(dest, source);
printf("The copied string is: %s\n", dest);
return 0;
}
这里的输出结果是Hello, world!
strncpy 函数
strncpy
函数用于将源字符串的前 n
个字符复制到目标字符串中,其原型如下
char *strncpy(char *dest, const char *source, size_t n);
参数 dest
是指向目标字符串的指针,source
是指向源字符串的指针,n
是要复制的字符数。strncpy
函数会复制 source
指向的字符串的前 n
个字符到 dest
指向的字符串中。如果 source
字符串的长度小于 n
,则 dest
字符串中剩下的位置会被填充空字符('\0'
)如果source
字符串的长度大于 n
只是将 source
的前 n
个字符复制到dest
的前n个字符,不自动添加'\0'
,也就是结果dest
不包括'\0'
,需要再手动添加一个'\0'
。函数返回 dest
指针。
使用方法示例:
#include <stdio.h>
#include <string.h>
int main()
{
char dest[20];
const char *source = "Hello, world!";
size_t n = 10;
strncpy(dest, source, n);
printf("The copied string is: %s\n", dest);
return 0;
}
在这个例子中,strncpy
函数只复制了 source
字符串的前 10 个字符到 dest
字符串中,因为 source
字符串的长度是 12,所以剩下的两个位置被填充了NULL
。
模拟实现
虽然 strcpy
和 strncpy
函数在标准库中已经提供,但我们可以模拟实现它们的基本版本。下面是一个简单的 strcpy
函数的模拟实现:
#include <std.h>
char *my_strcpy(char *dest, const char *source)
{
while (*source)
{
*dest = *source;
dest++;
source++;
}
*dest = '\0'; // 添加空字符
return dest;
}
int main()
{
char dest[20];
const char *source = "Hello, world!";
char *result = my_strcpy(dest, source);
printf("The copied string is: %s\n", result);
return 0;
}
对于 strncpy
,模拟实现可能会更加复杂,因为它需要处理复制特定数量字符的情况。标准库中的 strncpy
函数已经很好地处理了边界条件,如源字符串长度小于目标长度等。如果你需要实现 strncpy
,你需要考虑这些边界条件。
strcat和strncat
strcat 函数
strcat
函数用于将源字符串附加到目标字符串的末尾,其原型如下:
char *strcat(char *dest, const char *source);
参数 dest
是指向目标字符串的指针,source
是指向源字符串的指针。strcat
函数会将 source
指向的字符串附加到 dest
指向的字符串的末尾,直到遇到空字符('\0'
)。函数返回 dest
指针。
使用方法示例:
#include <stdio.h>
#include <string.h>
int main()
{
char dest[20] = "Hello, ";
const char *source = "world!";
strcat(dest, source);
printf("The concatenated string is: %s\n", dest);
return 0;
}
这里的输出结果是Hello, world!
strncat 函数
strncat
函数与 strcat
函数类似,不同之处在于它只附加源字符串的前 n
个字符到目标字符串的末尾,原型如下:
char *strncat(char *dest, const char *source, size_t n);
参数 dest
是指向目标字符串的指针,source
是指向源字符串的指针,n
是要附加的最大字符数。strncat
函数会将 source
指向的字符串的前 n
个字符附加到 dest
指向的字符串的末尾,或者直到遇到空字符('\0'
),以较小者为准。函数返回 dest
指针。
使用方法示例:
#include <stdio.h>
#include <string.h>
int main()
{
char dest[20] = "Hello, ";
const char *source = "world!";
size_t n = 3;
strncat(dest, source, n);
printf("The concatenated string is: %s\n", dest);
return 0;
}
这里的输出结果是Hello, wor
模拟实现
下面是一个简单的 strcat
函数的模拟实现示例:
char *my_strcat(char *dest, const char *source)
{
// 找到 dest 字符串的末尾
while (*dest) {
dest++;
}
// 从 source 复制字符到 dest 的末尾
while (*source) {
*dest = *source;
dest++;
source++;
}
*dest = '\0'; // 添加空字符
return dest;
}
// 使用示例
int main()
{
char dest[20] = "Hello, ";
const char *source = "world!";
my_strcat(dest, source);
printf("The concatenated string is: %s\n", dest);
return 0;
}
同样的,你也可以根据需要编写一个模拟实现 strncat
函数的版本,需要注意处理源字符串长度小于 n
的情况,以及确保在目标字符串的末尾添加空字符。
strcmp和strncmp
strcmp
strcmp
函数用于比较两个字符串是否相等。
原型:
int strcmp(const char *str1, const char *str2);
返回值:
- 如果两个字符串相等,返回 0。
- 如果第一个字符串小于第二个字符串,返回负值。
- 如果第一个字符串大于第二个字符串,返回正值。
那么如何判断两个字符串? 比较两个字符串中对应位置上字符ASCII码值的大小。
示例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2);
if (result == 0)
{
printf("Strings are equal.\n");
}
else if (result < 0)
{
printf("String 1 is less than String 2.\n");
}
else
{
printf("String 1 is greater than String 2.\n");
}
return 0;
}
这个例子会比较字符串 "hello" 和 "world"
,因为h
的ASCII码小于w
的ASCII码值,所以返回String 1 is less than String 2.
strncmp
strncmp
函数在 C 语言中用于比较两个字符串的前 n
个字符。这个函数在 <string.h>
头文件中定义,其原型如下:
int strncmp(const char *str1, const char *str2, size_t n);
参数说明:
str1
:指向要比较的第一个字符串的指针。str2
:指向要比较的第二个字符串的指针。n
:要比较的字符数。
strncmp
函数返回值:
- 如果两个字符串的前
n
个字符完全相等,返回 0。 - 如果
str1
在字典序上小于str2
,返回小于 0 的值。 - 如果
str1
在字典序上大于str2
,返回大于 0 的值。
使用方法示例:
#include <stdio.h>
#include <string.h>
int main() {
const char *str1 = "Hello, world!";
const char *str2 = "hello, earth!";
size_t n = 9;
int result = strncmp(str1, str2, n);
if (result == 0)
{
printf("Strings are equal");
}
else if (result < 0)
{
printf("str1 is less than str2\n");
}
else
{
printf("str1 is greater than str2\n");
}
return 0;
}
在这个简单的例子中,我们比较了字符串 str1
和字符串 str2
的前9个字符。根据比较的结果,我们打印出相应的信息。
模拟实现
下面是一个简单的 strcmp
函数的模拟实现示例:
#include <stdio.h>
#include <string.h>
int my_strcmp (const char * str1, const char * str2)
{
int ret = 0 ;
while(*str1 == *str2)
{
if(*str1 == '\0')
return 0;
str1++;
str2++;
}
return *str1-*str2;
}
strstr
函数用于在目标字符串中查找子字符串。这个函数在 <string.h>
头文件中定义。
原型:
char *strstr(const char *haystack, const char *needle);
参数:
haystack
:指向要搜索的字符串的指针。needle
:指向要查找的子字符串的指针。
返回值:
- 如果找到子字符串,返回指向子字符串第一个字符的指针。
- 如果未找到子字符串,返回
NULL
。
示例:
#include <stdio.h>
#include <string.h>
int main() {
const char *str = "Hello, world!";
const char *substr = "world";
const char *found = strstr(str, substr);
if (found != NULL) {
printf("Substring found at index: %lu\n", found - str);
} else {
printf("Substring not found.\n");
}
return 0;
}
输出结果为Substring found at index: 7
在这个例子中,我们使用 strstr
函数在字符串 str
中查找子字符串 substr
。如果找到子字符串,它会返回指向子字符串第一个字符的指针;如果没有找到,它会返回 NULL
。
模拟:
下面是一个简单的 strstr
函数的模拟实现:
#include <stdio.h>
// 模拟实现 strstr 函数
char *my_strstr(const char *haystack, const char *needle)
{
if (*needle == '\0')
{
return (char *)haystack; // 空字符串作为needle时,返回haystack
}
while (*haystack)
{
if (*haystack == *needle)
{
const char *p1 = haystack;
const char *p2 = needle;
while (*p1 == *p2 && *p2)
{
p1++;
p2++;
}
if (*p2 == '\0')
{
return (char *)haystack; // 找到匹配,返回当前haystack指针
}
}
haystack++;
}
return NULL; // 未找到匹配,返回NULL
}
int main()
{
const char *str = "Hello, world!";
const char *substr = "world";
char *found = my_strstr(str, substr);
if (found != NULL)
{
printf("Substring found at index: %lu\n", found - str);
} else
{
printf("Substring not found.\n");
}
return 0;
}
在这个模拟实现中,我们使用了一个 while
循环来遍历 haystack
字符串。如果当前字符与 needle
字符串的第一个字符相等,我们就继续比较后续字符。如果 needle
字符串的剩余部分为空(即 '\0'
),则表示找到匹配,返回当前的 haystack
指针。如果遍历完整个 haystack
字符串都没有找到匹配,则返回 NULL
。
strtok
strtok
是 C 语言中的一个字符串操作函数,它用于将一个字符串分解为一系列标记,每个标记由一个特定的分隔符分隔。这个函数在 string.h
库中定义。
strtok
的基本用法如下:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello, World!";
char *token;
token = strtok(str, ","); // 使用逗号作为分隔符
while (token != NULL)
{
printf("%s\n", token); // 输出分隔后的每个标记
token = strtok(NULL, ","); // 获取下一个标记
}
return 0;
}
在上述代码中,strtok
函数被用来将字符串 “Hello, World!” 分割成多个标记,每个标记由逗号分隔。这个程序会输出 “Hello” 和 “World!”。
strtok
函数的第一个参数是要分割的字符串,第二个参数是分隔符。如果第二个参数为 NULL,那么 strtok
会使用字符串中的所有分隔符。
需要注意的是,strtok
会修改原始字符串,所以如果你需要保留原始字符串,你需要先复制一份再进行操作。此外,使用 strtok
后,需要确保不再使用原始字符串进行后续操作,否则可能会引发未定义行为。