C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C语言字符函数和字符串函数

C语言进阶教程之字符函数和字符串函数

作者:CGod

C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者字符数组中,下面这篇文章主要给大家介绍了关于C语言进阶教程之字符函数和字符串函数的相关资料,需要的朋友可以参考下

前言

一、函数介绍

1.1 strlen

size_t strlen ( const char * str );

注:

#include <stdio.h>
int main()
{
	const char* str1 = "abcdef";
	const char* str2 = "bbb";
	if (strlen(str2) - strlen(str1) > 0)
	{
		printf("str2>str1\n");
	}
	else
	{
		printf("srt1>str2\n");
	}
	return 0;
}

输出结果:

1.2 strcpy

char* strcpy(char * destination, const char* source);

1.3 strcat

char * strcat ( char * destination, const char * source );

1.4 strcmp

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

1.5 strncpy

char * strncpy ( char * destination, const char * source, size_t num );

1.6 strncat

char * strncat ( char * destination, const char * source, size_t num );
#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;
}

输出结果:

1.7 strncmp

int strncmp ( const char * str1, const char * str2, size_t num );
#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;
}

输出结果:

1.8 strstr

char * strstr ( const char *str1, const char * str2);
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "This is a simple string";
	char* pch;
	pch = strstr(str, "simple");
	puts(pch);
	return 0;
}

1.9 strtok

char * strtok ( char * str, const char * sep );
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "- This, a sample string.";
	char* pch;
	printf("Splitting string \"%s\" into tokens:\n", str);
	pch = strtok(str, " ,.-");
	while (pch != NULL)
	{
		printf("%s\n", pch);
		pch = strtok(NULL, " ,.-");
	}
	return 0;
}

输出结果:

1.10 strerror

#include <stdio.h>
#include <string.h>
#include <errno.h>//必须包含的头文件
int main()
{
	FILE* pFile;
	pFile = fopen("unexist.ent", "r");
	if (pFile == NULL)
		printf("Error opening file unexist.ent: %s\n", strerror(errno));
	//errno: Last error number
	return 0;
}

输出结果:

1.11 字符分类函数

字符转换:

int tolower ( int c );
int toupper ( int c );
#include <stdio.h>
#include <ctype.h>
int main()
{
	int i = 0;
	char str[] = "Test String.\n";
	char c;
	while (str[i])
	{
		c = str[i];
		if (isupper(c))
			c = tolower(c);
		putchar(c);
		i++;
	}
	return 0;
}

输出结果:

1.12 memcpy

void * memcpy ( void * destination, const void * source, size_t num );
#include <stdio.h>
#include <string.h>
struct {
	char name[40];
	int age;
} person, person_copy;
int main()
{
	char myname[] = "Pierre de Fermat";
	/* using memcpy to copy string: */
	memcpy(person.name, myname, strlen(myname) + 1);
	person.age = 46;
	/* using memcpy to copy structure: */
	memcpy(&person_copy, &person, sizeof(person));
	printf("person_copy: %s, %d \n", person_copy.name, person_copy.age);
	return 0;
}

输出结果:

1.13 memmove

void * memmove ( void * destination, const void * source, size_t num );
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "memmove can be very useful......";
	memmove(str + 20, str + 15, 11);
	puts(str);
	return 0;
}

输出结果:

1.14 memcmp

int memcmp ( const void * ptr1, 
 const void * ptr2, 
 size_t num );

#include <stdio.h>
#include <string.h>
int main()
{
	char buffer1[] = "DWgaOtP12df0";
	char buffer2[] = "DWGAOTP12DF0";
	int n;
	n = memcmp(buffer1, buffer2, sizeof(buffer1));
	if (n > 0) printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
	else if (n < 0) printf("'%s' is less than '%s'.\n", buffer1, buffer2);
	else printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
	return 0;
}

输出结果:

二、库函数的模拟实现

2.1 模拟实现strlen

三种方式:

方式1:

//计数器方式
int my_strlen(const char* str) 
{
	int count = 0;
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}

方式2:

//不能创建临时变量计数器
int my_strlen(const char * str) 
{
	if(*str == '\0')
		return 0;
	else
		return 1+my_strlen(str+1);
}

方式3:

//指针-指针的方式
int my_strlen(char *s) 
{
    char *p = s;
    while(*p != ‘\0' )
        p++;
    return p-s; 
}

2.2 模拟实现strcpy

参考代码:

//1.参数顺序
//2.函数的功能,停止条件
//3.assert
//4.const修饰指针
//5.函数返回值
//6.题目出自《高质量C/C++编程》书籍最后的试题部分
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest != NULL);
	assert(src != NULL);
	while ((*dest++ = *src++))
	{
		;
	}
	return ret;
}

2.3 模拟实现strcat

参考代码:

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;
}

2.4 模拟实现strstr

参考代码:

char* strstr(const char* str1, const char* str2) 
{
	char* cp = (char*)str1;
	char* s1, * s2;
	if (!*str2)
		return((char*)str1);
	while (*cp)
	{
		s1 = cp;
		s2 = (char*)str2;
		while (*s1 && *s2 && !(*s1 - *s2))
			s1++, s2++;
		if (!*s2)
			return(cp);
		cp++;
	}
	return(NULL);
}

2.5 模拟实现strcmp

参考代码:

int my_strcmp(const char* src, const char* dst) 
{
	int ret = 0;
	assert(src != NULL);
	assert(dest != NULL);
	while (!(ret = *(unsigned char*)src - *(unsigned char*)dst) && *dst)
		++src, ++dst;
	if (ret < 0)
		ret = -1;
	else if (ret > 0)
		ret = 1;
	return(ret);
}

2.6 模拟实现memcpy

参考代码:

void* memcpy(void* dst, const void* src, size_t count) 
{
	void* ret = dst;
	assert(dst);
	assert(src);
	/*
	 * copy from lower addresses to higher addresses
	 */
	while (count--) {
		*(char*)dst = *(char*)src;
		dst = (char*)dst + 1;
		src = (char*)src + 1;
	}
	return(ret);
}

2.7 模拟实现memmove

参考代码:

void* memmove(void* dst, const void* src, size_t count) 
{
	void* ret = dst;
	if (dst <= src || (char*)dst >= ((char*)src + count)) 
	{
		/*
		 * Non-Overlapping Buffers
         * copy from lower addresses to higher addresses
         */
		while (count--) {
			*(char*)dst = *(char*)src;
			dst = (char*)dst + 1;
			src = (char*)src + 1;
		}
	}
	else {
		/*
		 * Overlapping Buffers
		 * copy from higher addresses to lower addresses
		 */
		dst = (char*)dst + count - 1;
		src = (char*)src + count - 1;
		while (count--) {
			*(char*)dst = *(char*)src;
			dst = (char*)dst - 1;
			src = (char*)src - 1;
		}
	}
	return(ret);
}

总结

到此这篇关于C语言进阶教程之字符函数和字符串函数的文章就介绍到这了,更多相关C语言字符函数和字符串函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文