日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言中操作字符串的函數詳解_C 語言

作者:朱國鑫 ? 更新時間: 2022-05-25 編程語言

一、函數表

函數名 函數 功能
strlen size_t strlen(const char* s); 返回字符串 s 的長度(不包括結尾的0)
strcmp int strcmp(const char* s1, const char* s2); 比較兩個字符串,返回:如果 s1 == s2,返回 0;如果 s1s2 則返回大于 0 (如 1)
strcpy char* strcpy(char* restrict dst, const char* reestrict src) 把字符串 src 復制拷貝到字符串 dst,返回 dst,restrict 表明 src 和 dst 不能重疊
strcat char* stract(char* restrict s1, const char* reestrict s2) 把字符串 s2 拷貝到字符串 s1 的后面,連接成一個長的字符串,返回 s1
strchr char* strchr(const char* s, int c) 返回一個指針,指向字符串 s1 中字符 ch 的第一次出現的位置
strstr char* strstr(const char* s1, const char* s2) 返回一個指針,指向字符串 s1 中字符串 s2 的第一次出現的位置

二、strlen

實例

#include 
#include 
//自定義 strlen()函數
size_t mylen(const char* s)
{
	int idx = 0;
	while (s[idx] != '\0') {
		idx++;
	}
	return idx;
}
int main()
{
	char a[] = "Hello";
	printf("mylen=%llu\n", mylen(a));
	printf("strlen=%llu\n", strlen(a));// {'H', 'e', 'l', 'l', 'o'};
	printf("sizeof=%llu\n", sizeof(a));// {'H', 'e', 'l', 'l', 'o','\0'};
	return 0;
}

運行結果

在這里插入圖片描述

三、strcmp

實例

#include 
#include 
//自定義 strcmp()函數
int mycmp1(const char* s1, const char* s2)
{
	int idx = 0;
	while (1) {
		if (s1[idx] != s2[idx]) {
			break;
		}
		else if (s1[idx] == '\0') {
			break;
		}
		idx++;
	}
	return s1[idx] - s2[idx];
}
int mycmp2(const char* s1, const char* s2)
{
	int idx = 0;
	while (s1[idx] == s2[idx] && s1[idx] != 0) {
		idx++;
	}
	return s1[idx] - s2[idx];
}
int mycmp3(const char* s1, const char* s2)
{
	int idx = 0;
	while (*s1 == *s2 && *s1 != 0) {
		s1++;
		s2++;
	}
	return *s1 - *s2;
}
int main()
{
	char s1[] = "abc";
	char s2[] = "abc";
	char s3[] = "abc ";
	char s4[] = "bbc";
	char s5[] = "Abc";
	printf("mycmp1=%d\n", mycmp1(s1, s2));
	printf("mycmp2=%d\n", mycmp2(s1, s3));
	printf("mycmp3=%d\n", mycmp3(s1, s4));
	printf("mycmp3=%d\n", mycmp3(s1, s5));
	printf("strlen=%d\n", strcmp(s1, s2));
	printf("strlen=%d\n", strcmp(s1, s3));
	printf("strlen=%d\n", strcmp(s1, s4));
	printf("strlen=%d\n", strcmp(s1, s5));
	return 0;
}

運行結果

VS編輯器的原因,庫函數 strlen 的結果只有 0, -1, 1 三種結果

在這里插入圖片描述

四、strcpy

// 復制一個字符串
char* dst = (char*)malloc(strlen(src)+1);
strcpy(dst,src);
// strlen 查 src 大小 +1 (+1 的原因是加上字符串最后的 ‘\0', strlen 讀取的長度不包括 ‘\0'), 再 malloc 分配空間給 dst, 這樣 dst 的空間正好等于 src, 然后再執行 strcpy 復制操作

實例

#include 
#include 
//自定義 strcpy()函數
char* mycpy1(char* dst, const char* src)
{
	int idx = 0;
	while (src[idx] != '\0') {
		dst[idx] = src[idx];
		idx++;
	}
	dst[idx] = '\0';
	return dst;
}
char* mycpy2(char* dst, const char* src)
{
	char* ret = dst;
	while (*src) {
		*dst++ = *src++;
	}
	*dst = '\0';
	return ret;
}
int main()
{
	char s1[] = "abc";
	char s2[] = "def";
	char s3[] = "ghi";
	char s4[] = "jkl";
	printf("s1=%s\n", s1);
	strcpy(s1, s2);
	printf("s1=%s\n", s1);
	mycpy1(s1, s3);
	printf("s1=%s\n", s1);
	mycpy2(s1, s4);
	printf("s1=%s\n", s1);
	return 0;
}

運行結果

在這里插入圖片描述

五、stract

實例

#include 
#include 
int main()
{
	char s1[10] = "abc";
	char s2[] = "def";
	printf("s1=%s\n", s1);
	strcat(s1, s2);
	printf("s1=%s\n", s1);
	return 0;
}

運行結果

在這里插入圖片描述

六、strchr

實例

#include 
#include 
int main()
{
	char s1[10] = "abcdebcde";
	printf("%p\n", s1);
	printf("%p\n", s1 + 1);
	char* p = strchr(s1, 'b');
	printf("%s\n", p);
	printf("%p\n", p);
	return 0;
}

運行結果

在這里插入圖片描述

總結

原文鏈接:https://blog.csdn.net/weixin_46398948/article/details/123647742

欄目分類
最近更新