CSDN的各位友友们你们好,今天千泽为大家带来的是C语言中字符函数和字符串函数的详解,掌握了这些内容能够让我们更加灵活的运用字符串,
接下来让我们一起走进今天的内容吧!
写这篇文章需要在 cplusplus.com 上大量截图,十分不易!
如果对您有帮助的话希望能够得到您的支持和帮助,我会持续更新的!
0.前言
C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在
常量字符串中或者字符数组中。
字符串常量适用于那些对它不做修改的字符串函数.
首先,我们将本次要介绍的函数分一下类
长度不受限制的字符串函数
strcpy
strcat
strcmp
长度受限制的字符串函数介绍
strncpy
strncat
strncmp
1.函数介绍
🚀strcpy
例子
/* strcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
输出:
str1: Sample string
str2: Sample string
str3: copy successful
🚩模拟实现strcpy
#include<stdio.h>
#include<string.h>
//1.参数顺序
//2.函数的功能,停止条件
//3.assert
//4.const修饰指针
//5.函数返回值
//6.题目出自《高质量C/C++编程》书籍最后的试题部分
//返回的是目标空间的起始地址
#include<assert.h>
char * my_strcpy(char * dest, const char* src)
{
char * ret = dest;
assert(dest!=NULL);
assert(src != NULL);
while ((*dest++ = *src++))
{
;
}
return ret;
}
int main()
{
char arr1[] = "hehe";
char arr2[20] = { 0 };
my_strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
友友们记得动手实践!!!
🚀strcat
官方例子
/* strcat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;
}
输出
these strings are concatenated.
🚩模拟实现strcat
#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
char* ret = dest;
assert(dest != NULL);
assert(src != NULL);
//找目标空间的\0
while (*dest!='\0')
{
dest++;
}
//拷贝
while ((*dest++ = *src++))
{
;
}
return ret;
}
int main()
{
char arr1[20] = "hello ";
char arr2[] = "world";
//追加
my_strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
🚀strcmp
官方例子
#include <stdio.h>
#include <string.h>
int main ()
{
char key[] = "apple";
char buffer[80];
do {
printf ("Guess my favorite fruit? ");
fflush (stdout);
scanf ("%79s",buffer);
} while (strcmp (key,buffer) != 0);
puts ("Correct answer!");
return 0;
}
输出
Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!
🚩模拟实现strcmp
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 != NULL);
assert(str2 != NULL);
while (*str1 == *str2)
{
if (*str1 == '\0')
{
return 0;
}
str1++;
str2++;
}
if (*str1 > *str2)
{
return 1;
}
else
{
return -1;
}
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "bbcdef";
int ret = my_strcmp(arr1, arr2);
printf("%d\n", ret);
return 0;
}
🚀strncpy
官方例子
/* strncpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
/* copy to sized buffer (overflow safe): */
strncpy ( str2, str1, sizeof(str2) );
/* partial copy (only 5 chars): */
strncpy ( str3, str2, 5 );
str3[5] = '\0'; /* null character manually added */
puts (str1);
puts (str2);
puts (str3);
return 0;
}
输出
To be or not to be To be or not to be To be
🚩模拟实现strncpy
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
char arr1[] = "abcdef";
char arr2[5] = { 0 };
strncpy(arr2, arr1, 3);
printf("%s\n", arr2);
return 0;
}
🚀strncat
官方例子
/* strncat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
puts (str1);
return 0;
}
输出
To be or not
🚩模拟实现strncat
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
char arr1[20] = "hello \0xxxxxxxx";
char arr2[] = "world";
strncat(arr1, arr2, 3);
printf("%s\n", arr1);
return 0;
}
🚀strncmp
官方例子
/* strncmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
puts ("Looking for R2 astromech droids...");
for (n=0 ; n<3 ; n++)
if (strncmp (str[n],"R2xx",2) == 0)
{
printf ("found %s\n",str[n]);
}
return 0;
}
输出
Looking for R2 astromech droids... found R2D2 found R2A6
🚩模拟实现strncmp
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abcq";
int ret = strncmp(arr1, arr2, 4);
printf("%d\n", ret);
return 0;
}
好了,今天的分享暂时告一段落,预知后续请看下篇😁,祝大家学习进步!