C和指针 第九章代码

1.

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int  n_cntrl;
int  n_space;
int  n_digit;
int  n_lower;
int  n_upper;
int  n_punct;
int  n_nprint;
int  total;
int
main()
{
	int  ch;
	int  category;
	
	while ((ch = getchar()) != EOF) {
		total += 1;
		
		if (iscntrl(ch))
			n_cntrl += 1;
		if (isspace(ch))
			n_space += 1;
		if (isdigit(ch))
			n_digit += 1;
		if (islower(ch))
			n_lower += 1;
		if (isupper(ch))
			n_upper += 1;
		if (ispunct(ch))
			n_punct += 1;
		if (!isprint(ch))
			n_nprint += 1;
	}
	
	if (total == 0)
		printf("No characters in the input!\n");
	else {
		printf("%3.0f%% %s control characters\n",
			n_cntrl * 100.0 / total);
		printf("%3.0f%% %s whitespace characters\n",
			n_space * 100.0 / total);
		printf("%3.0f%% %s digit characters\n",
			n_digit * 100.0 / total);
		printf("%3.0f%% %s lower case characters\n",
			n_lower * 100.0 / total);
		printf("%3.0f%% %s upper case characters\n",
			n_upper * 100.0 / total);
		printf("%3.0f%% %s punctuation characters\n",
			n_punct * 100.0 / total);
		printf("%3.0f%% %s non–printable characters\n",
			n_nprint * 100.0 / total);
	}
	return 0;
}

 

2.

size_t my_strlen(char const *string, int size) {
	register size_t length;
	for (length = 0; length < size; length += 1) {
		if (*string++ == '\0') {
			break;
		}
	}
	return length;
}

3.

#include <string.h>
char * my_strcpy(char *dst, char const *src, int size)
{
	strncpy(dst, src, size);
	dst[size -1] = '\0';
	return dst;
}

4。

size_t my_strlen(char const *string, int size) {
	register size_t length;
	for (length = 0; length < size; length += 1) {
		if (*string++ == '\0') {
			break;
		}
	}
	return length;
}

#include <string.h>

char * my_strcat(char *dst, char const *src, int size)
{
	int  length;
	size-= 1;
	length = size-(int)my_strlen(dst, size);
	if (length > 0) {
		strncat(dst, src, length);
		dst[size] = '\0';
	}
	return dst;
}

 

5.

void my_strncat(char *dest, char *src, int dest_len)
{
	register int  len;
	
	len = strlen(dest);
	dest_len -= len + 1;
	
	if( dest_len > 0 )
	strncat( dest + len, src, dest_len );
}

6.

char *my_strcpy_end(register char *dst, register char const *src) {
	while ((*dst++==*src++)!='\0')
	{
		;
	}
	return dst - 1;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值