C语言库函数——string.h

目录

摘要:

一、头文件 string.h 中定义的函数

strcpy

用法:

代码示例:

参数:

memchr

用法:

代码示例:

参数:

strlen

用法:

代码示例:

strcat

用法:

代码示例:

参数:

strcmp

用法:

代码示例:

参数:

memcmp

用法:

代码示例:

参数:

strcpy


摘要:

 在学习C语言的过程中,当使用字符串时,我们会经常使用 string.h 这个库函数。那么,这个库函数究竟有哪些需要我们学习的,哪些又是我们经常会使用的。string.h 是C语言标准库中一个常用的头文件,在使用到字符数组时需要使用。string .h 头文件定义了一个变量类型、一个宏和各种操作字符数组的函数。


一、头文件 string.h 中定义的函数

  • strcpy

用法:

拷贝一个字符串到另一个

代码示例:

char *strcpy(char *destin, char *src)

参数:

  • dest -- 这就是指针的内容将被复制到目标数组。

  • src -- 这是要复制的字符串。

#include 
#include 

int main()
{
	char name[]="CSDN_Author:Xa_L";
	char time[50];
	strcpy(time,name);
	printf("%s",time);
	return 0;
}

  • memchr

用法:

拷贝一个字符串到另一个

代码示例:

void *memchr(const void *str, int c, size_t n) 

参数:

  • str -- 这是指针内存块执行搜索。

  • c -- 这是要传递一个int值,但功能进行搜索使用无符号字符型转换这个值每字节一个字节。

  • n -- 这是要分析的字节数。

#include 
#include 

int main()
{
	char name[]="CSDN_Author:Xa_L";
	char ch='A';
	char ret;
	ret = memchr(name, ch, strlen(name));
	if(ret != EOF)
	{
		printf("Success found %c",ch);
	}
	else
	{
		printf("No success");
	}
	return 0;
}

  • strlen

用法:

用于计算字符串的长度。

代码示例:

size_t strlen(const char *str)

#include 
#include 

int main()
{
    int len;
	char name[]="CSDN_Author:Xa_L";
	len = strlen(name);
	printf("%d",len);
	return 0;
}

  • strcat

用法:

将两个字符串连接(拼接)起来。

代码示例:

char *strcat(char *dest, const char *src)

参数:

  • dest -- 指向目标数组,该数组包含了一个 C 字符串,且足够容纳追加后的字符串。
  • src -- 指向要追加的字符串,该字符串不会覆盖目标字符串。
#include
#include 
int main()
{
    char text[50],copy[50];
    strcpy(text,"The is one,");
    strcpy(copy,"the is two");
    strcat(text,copy);
    printf("%s",text);
    return 0;
}

  • strcmp

用法:

用于比较两个字符串是否一样,这里对其进行比较的是ASCII 值。

代码示例:

int strcmp(const char *str1, const char *str2)

参数:

  • str1 -- 要进行比较的第一个字符串。
  • str2 -- 要进行比较的第二个字符串。
#include
#include 
int main()
{
    char text[50],copy[50];
    strcpy(text,"The is one,");
    strcpy(copy,"the is two");
    if( strcmp(text,copy)> 0 )
    {
        printf("text 字符串ASCII 值大于 copy 字符串ASCII 值");
    }
    else if(strcmp(text,copy) < 0)
    {
        printf("text 字符串ASCII 值小于 copy 字符串ASCII 值");
    }
    else
    {
        printf("text 字符串ASCII 值等于 copy 字符串ASCII 值");
    }
    return 0;
}

  • memcmp

用法:

对字符串str1和str2的前n个字符进行比较

代码示例:

int memcmp(const void *str1, const void *str2, size_t n)

参数:

  • str1 -- 指向内存块的指针。
  • str2 -- 指向内存块的指针。
  • n -- 要被比较的字节数。
#include
#include 
int main()
{
    char text[50],copy[50];
    strcpy(text,"The is one,");
    strcpy(copy,"the is two");
    if( memcmp(text,copy,4)> 0 )
    {
        printf("text 字符串长度大于 copy 字符串长度");
    }
    else if(memcmp(text,copy,4) < 0)
    {
        printf("text 字符串长度小于 copy 字符串长度");
    }
    else
    {
        printf("text 字符串长度等于 copy 字符串长度");
    }
    return 0;
}

注意:memcmp和strcmp的用法基本上是一样的,只不过一个是比较整串字符串的值,而另一个比较的是自己定义的前n个值。


 上面是我认为在该库函数中比较重要的一些用法,以后会往里面进行添加。

技术交流
欢迎转载、收藏、有所收获点赞支持一下!

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAWGFfTA==,size_20,color_FFFFFF,t_70,g_se,x_16

 

你可能感兴趣的:(编程语言开发,#,C语言编程算法,c语言,string.h,库函数)