字符串函数

1. Strcpy函数

#include <stdio.h>
#include <string.h>
int main(void){
	char boy_Name1[20];
	const char* boy_Name2 = "Mingming ";
	strcpy_s(boy_Name1, boy_Name2);
	printf("%s", boy_Name1);
}

2. Strlen函数

#include <stdio.h>
#include <string.h>

int main(void){
	const char* boy_Name = "Chu mingming ";
	int name_Length = strlen(boy_Name);
	printf("%d", name_Length);
}

3. Strcmp函数

#include <stdio.h>
#include <string.h>

int main(void){
	const char* string_1 = "ABCDE";
	const char* string_2 = "FGHIJ";
	int result = strcmp(string_1, string_2);
	if (result < 0)
		printf("string_1 is less than string_2");
	else if (result > 0)
		printf("string_1 is more than string_2");
	else
		printf("string_1 is equal to string_2");
	}

4. strcat函数

#include <stdio.h>
#include <string.h>

int main(void){
	char first_Word[20] = "Hello";
	const char* blank = " ";
	const char* second_Word = "World";
	strcat_s(first_Word, blank);
	strcat_s(first_Word, second_Word);
	printf("%s", first_Word);
	}

5. strchr函数

int main(void){
	char str1[] = "20220225";       // 字符串str1
	char str2[] = "50";             // 字符串str2
	char* ret1;                     // 声明strpbrk的返回值
 	ret1 = strpbrk(str1, str2);     // 第一个两字符串都包含的字符在str1中位置的指针
	int r1 = ret1 - str1;           // 指针ret1减去str1字符首地址 得到共有字符的位置
 	printf("%c,%p\n", *ret1,ret1); // 打印共有字符
	printf("%c\n", *(str1+r1));     // 打印共有字符
	}

**6.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    long ulCmpRet1;
    long ulCmpRet2;
    char *string_A ="This is string A";
    char *string_a ="This is string a";
    char *string_B ="This is string B";

    ulCmpRet1 = strcasecmp(string_A,string_a);
    ulCmpRet2 = strcasecmp(string_A,string_B);
    printf("%ld\n",ulCmpRet1);
    printf("%ld\n",ulCmpRet2);
}

posted on 2022-02-25 12:07  轩邈、  阅读(53)  评论(0编辑  收藏  举报

导航