目录
1. 字符分类函数
此类函数是针对单个字符而言的,判断一个字符是什么类型的,
全需包含<ctype.h>头文件
2. 字符转换函数
字符转化函数简而言之,就是对一个字符进行转化,共有两种。
依旧需要包含<ctype.h>头文件。
int c tolower (int c ); //将字符转化为小写
int c toupper (int c ); //将字符转化为大写
PS:判断是否为大小写是is,转化大小写是to。
3. strlen的使用及模拟实现(与sizeof区别)
strlen
size_t strlen ( const char * str );
Get string lengthReturns the length of the C string str.
The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself). 来自于cplusplus
1.就是获得字符串的长度 ,字符串以 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前⾯出现的字符个数(不包含 '\0' )。
2.注意函数的返回值为size_t,是无符号的( 易错 )
3.字符串函数都要包含头文件<string.h>
使用:
#include<stdio.h>
#include<string.h>
int main()
{
const char *ptr = "abcdef";
printf("%d\n",strlen(*ptr)); //结果是6
return 0;
}
模拟实现:
1.计数器方法
size_t my_strlen(const char* ptr)
{
int count = 0;
assert(ptr); //断言,确保*ptr是有效指针
while (*ptr)
{
count++;
*ptr++;
}
return count;
}
2.递归函数方法(不创建临时变量)
size_t my_strlen(const char* ptr)
{
assert(ptr); //断言,确保*ptr是有效指针
if (*ptr == '\0')
{
return 0;
}
while (*ptr)
{
return 1 + my_strlen(ptr + 1);
}
}
3.指针减指针
size_t my_strlen(const char* ptr)
{
assert(ptr); //断言,确保*ptr是有效指针
char* cpy = ptr;
while (*cpy != '\0')
{
cpy++;
}
return cpy - ptr;
}
strlen和sizeof在计算字符长度区别
1.首先strlen是一个函数,使用其必须包含头文件<string.h>
而sizeof是运算符,用于获取数据类型或变量在内存中所占用的字节数。
2.strlen
函数用于计算字符串的长度,即字符串中的字符数,不包括字符串末尾的空字符 ‘\0’。
sizeof
函数用于计算字符串的大小,也即字符串中的字符数,但包括字符串末尾的空字符 ‘\0’。
所以sizeof计算长度是会+1(即‘\0’长度)
4. strcpy 的使用和模拟实现
strcpy
char * strcpy ( char * destination, const char * source );
Copy string
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.Parameters
destination
Pointer to the destination array where the content is to be copied.
source
C string to be copied
Return Value
destination is returned.
来自cplusplus
1.用于将一个字符串复制到另一个字符串中
2.源字符串必须以 '\0' 结束。
3.会将源字符串中的 '\0' 拷⻉到⽬标空间。
4.⽬标空间必须⾜够⼤,以确保能存放源字符串 。
模拟实现:
char* my_strcpy(char* dest, char* src)
{
assert(dest && src);
char* ret = dest;
/*while (*src)
{
*ret = *src;
src++;
ret++;
}*/ //最常规方法
while (*ret++ = *src++)//当src指向\0时,此表达式的值为零停止
{
;//空语句
}
return dest;
}
5. strcat的使用和模拟实现
strcat
char * strcat ( char * destination, const char * source );
Concatenate stringsAppends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
destination and source shall not overlap.Parameters
destination
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
source
C string to be appended. This should not overlap destination
Return Value
destination is returned.
1. strcat
用于连接(即,拼接)两个字符串。它的作用是将源字符串的内容附加到目标字符串的末尾。
2.源字符串必须以 '\0' 结束。 •
3.目标字符串中也得有 \0 ,否则没办法知道追加从哪⾥开始。
4.目标空间必须有⾜够的⼤,能容纳下源字符串的内容。
模拟实现:
char *my_strcat(char *dest, const char*src)
{
char *ret = dest;
assert(dest != NULL);
assert(src != NULL);
while(*dest)
{
dest++;
}
while((*dest++ = *src++))
{
;
}
return ret;
}
ps:当自己和自己连接时候,会怎么样呢?
会死循环。
因为在运行过程中原先的‘\0’会被替换掉,导致此后的都不会出现‘\0’导致无法结束。
6.strcmp的使用和模拟实现
strcmp
int strcmp ( const char * str1, const char * str2 );
Compare two stringsCompares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll.Parameters
str1
C string to be compared.
str2
C string to be compared.
Return Value
Returns an integral value indicating the relationship between the strings:
return value indicates <0
the first character that does not match has a lower value in ptr1 than in ptr2 0
the contents of both strings are equal >0
the first character that does not match has a greater value in ptr1 than in ptr2
strcmp
是 C 语言中的一个函数,用于比较两个字符串。它会按照字典顺序比较两个字符串,并返回一个整数来表示比较结果。具体来说:
- 如果第一个字符串小于第二个字符串,则返回一个负数。
- 如果第一个字符串大于第二个字符串,则返回一个正数。
- 如果两个字符串相等,则返回 0。
ps:比较的不是字符的长度,而是按照字符的顺序一个一个比较字符的大小
模拟实现;
int my_strcmp (const char * str1, const char * str2)
{
int ret = 0 ;
assert(src != NULL);
assert(dest != NULL);
while(*str1 == *str2)
{
if(*str1 == '\0')
return 0;
str1++;
str2++;
}
return *str1-*str2;
}
7. strncpy strncat strncmp的使用
这三个函数和strcpy strcat strcmp使用方法基本一致,但是其多了一个n,并且在调用函数是多一个参数,其用来限制具体操作的字符的个数,下来具体讲解:
strncpy
char * strncpy ( char * destination, const char * source, size_t num );
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
拷贝num个字符从源字符串到目标空间。
如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
如果源字符串的长度大于num,则拷贝num个字符之后,不会目标的后边追加‘\0’
strnca
char * strncat ( char * destination, const char * source, size_t num );
Appends the first num characters of source to destination, plus a terminating null-character.
If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
1.将source指向字符串的前num个字符追加到destination指向的字符串末尾,再追加⼀个 \0字符
2.如果source 指向的字符串的⻓度⼩于num的时候,只会将字符串中到 \0 的内容追加destination指向的字符串末尾
strncmp
Compares up to num characters of the C string str1 to those of the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
⽐较str1和str2的前num个字符,如果相等就继续往后⽐较,最多⽐较num个字⺟,如果提前发现不⼀ 样,就提前结束,⼤的字符所在的字符串⼤于另外⼀个。如果num个字符都相等,就是相等返回0.
8.strstr函数的使用和模拟实现
strstr
const char * strstr ( const char * str1, const char * str2 ); char * strstr ( char * str1, const char * str2 );
Locate substringReturns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. 函数返回字符串str2在字符串str1中第⼀次出现的位置
字符串的⽐较匹配不包含 \0 字符,以 \0 作为结束标志
The matching process does not include the terminating null-characters, but it stops there.Parameters
str1
C string to be scanned.
str2
C string containing the sequence of characters to match.
Return Value
A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.
strstr
用于在一个字符串中查找指定子字符串的第一次出现位置。如果找到了子字符串,则返回指向该位置的指针;如果未找到,则返回 NULL。
模拟实现:
char* my_strstr(char* str1,const char* str2)
{
char* ret = str1;
char* s1;
char* s2;
if (*str2 == '\0')
return(str1);
while (*ret)
{
s1 = ret;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return ret;
ret++;
}
return NULL;
}
9.strtok的使用
strtok
char * strtok ( char * str, const char * delimiters );
Split string into tokensA sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.
On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of the last token as the new starting location for scanning.
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token. The scan also stops if the terminating null character is found.
This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function.
Once the terminating null character of str is found in a call to strtok, all subsequent calls to this function (with a null pointer as the first argument) return a null pointer.
The point where the last token was found is kept internally by the function to be used on the next call (particular library implementations are not required to avoid data races).Parameters
str
C string to truncate.
Notice that this string is modified by being broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.delimiters
C string containing the delimiter characters.
These can be different from one call to another.Return Value
If a token is found, a pointer to the beginning of the token.
Otherwise, a null pointer.
A null pointer is always returned when the end of the string (i.e., a null character) is reached in the string being scanned.
• sep参数指向⼀个字符串,定义了⽤作分隔符的字符集合
• 第⼀个参数指定⼀个字符串,它包含了0个或者多个由sep字符串中⼀个或者多个分隔符分割的标 记。
• strtok函数找到str中的下⼀个标记,并将其⽤ \0 结尾,返回⼀个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使⽤strtok函数切分的字符串⼀般都是临时拷⻉的内容 并且可修改。)
• strtok函数的第⼀个参数不为 NULL ,函数将找到str中第⼀个标记,strtok函数将保存它在字符串 中的位置。
• strtok函数的第⼀个参数为 NULL ,函数将在同⼀个字符串中被保存的位置开始,查找下⼀个标 记。
• 如果字符串中不存在更多的标记,则返回 NULL 指针。
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "192.168.6.111";
char* sep = ".";
char* str = NULL;
for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
// 找到第一个符号位置 判断是否还能继续找到 寻找后续的位置
{
printf("%s\n", str);
}
return 0;
}
这个函数不太容易理解,但是可以直接记住如何去用。
感谢阅读至此,如有错误,请在评论区支出,或者有什么疑问,可以在评论区提问,我看到会立即回复。