第十章习题

10.19

//第十九题
#include <stdio.h>

char* mygets(char* buf);
int myputs(char* cstr);

main()
{
	char s[99];
	printf("请输入字符串:");
	mygets(s);
	printf("输出字符串:");
	myputs(s);
}

char* mygets(char *buf)
{
	int c;
	char *s;

	for (s = buf; (c = getchar()) != '\n';)
		if (c == EOF)
			if (s == buf)
				return (NULL);
			else
				break;
		else
			*s++ = c;
	*s = '\0';
	return (buf);
}

int myputs(char* cstr)
{
	int i = 0;
	while ('\0' != cstr[i++])
	{
		putchar(cstr[i-1]);
	}
	putchar('\n');
	return i;
}


10.20

//第二十题
#include <stdio.h>

int fun(char* s);

main()
{
	char str[99];
	puts("请输入一字符串:");
	gets(str);
	puts("输出该字符串:");
	puts(str);
	if (1 == fun(str))
	{
		printf("%s 是回文。\n", str);
	} 
	else
	{
		printf("%s 不是回文。\n", str);
	}
}

//判断字符串是否是回文
int fun(char* s)
{
	int i = 0;
	int j = strlen(s)-1;
	while (i<j)
	{
		if (s[i++] != s[j--])
		{
			return 0;
		}
	}
	return 1;
}


10.21

//第二十一题
#include <stdio.h>

char del_char(char* str, int i);

main()
{
	char s[99];
	int i;
	char c;
	printf("请输入一串字符:");
	gets(s);
	printf("输出该串字符:");
	puts(s);
	printf("输入一整数:");
	scanf("%d", &i);
	if (NULL != (c = del_char(s, i)))
	{
		printf("删除第%d个字符成功,删除后的字符串为:%s\n", i, s);
		printf("删除的字符为:%c", c);
	}
	else
	{
		printf("删除不成功。\n");		
	}
	
}

//删除字符串中的第i个元素
char del_char(char* str, int i)
{
	char ch;
	if (i < 0 || i >= strlen(str))
	{
		return NULL;
	} 
	else
	{
		ch = str[i];
		while (i < strlen(str))
		{
			str[i-1] = str[i];
			++i;
		}
		str[i-1] = '\0';
		return ch;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值