C语言程序设计实例26至30

C语言程序设计实例26

字符数组s1、s2,把s2在s1中出现的字符全部删除。

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


int main()
{
	char s1[50], s2[50], *p1, *p2;
	int i, j;
	p1 = s1;
	p2 = s2;
	printf("s1=");
	scanf("%s", p1);
	printf("s2=");
	scanf("%s", p2);
	while (*p2)
	{
		for (i = 0, j = 0; *(p1 + i) != '\0';i++)
		{
			if (*(p1 + i) != *p2)
			{
				*(p1 + (j++)) = *(p1 + i);
			}
		}
		*(p1 + j) = '\0';
		p2++;
	}
	puts(p1);
	system("pause");
	return 0;
}

C语言程序设计实例27

将b字符串连接到a字符串后面,编写函数实现此功能

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


int main()
{
	char s1[100], s2[50], *p1, *p2;
	p1 = s1;
	p2 = s2;
	printf("s1=");
	scanf("%s", p1);
	printf("s2=");
	scanf("%s", p2);
	while (*p1++);
	p1--;
	while (*p2)
	{
		*p1++= *p2++;
	}
	*p1 ='\0';
	puts(s1);
	system("pause");
	return 0;
}

C语言程序设计实例28

将b字符串复制到a中,编写函数实现此功能

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


int main()
{
	char s1[100], s2[50], *p1, *p2;
	p1 = s1;
	p2 = s2;
	printf("s2=");
	scanf("%s", p2);
	while (*p2)
	{
		*p1++= *p2++;
	}
	*p1 ='\0';
	puts(s1);
	system("pause");
	return 0;
}

C语言程序设计实例29

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


int main()
{
	char s1[100], *p1;
	int space=0, letters=0, num=0, others=0;
	p1 = s1;
	printf("s1=");
	scanf("%s", p1);

	while (*p1)
	{
		if (*p1 >= 'a'&&*p1 <= 'z' || *p1 >= 'A'&&*p1 <= 'Z')
		{
			letters++;
		}
		else if (*p1 >= '0'&&*p1 <= '9')
		{
			num++;
		}
		else if (*p1 == ' ')
		{
			space++;
		}
		else
		{
			others++;
		}
		p1++;
	}
	printf("\nletters=%d", letters);
	printf("\nnum=%d", num);
	printf("\nspace=%d", space);
	printf("\nothers=%d", others);
	system("pause");
	return 0;
}

C语言程序设计实例30

打印出如下图形:
*
***
*****
***
*

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


int main()
{
	int x, i, j, n = 3;
	for (i = 0; i < 3; i++)
	{
		for (j = n--; j > 0; j--)
		{
			printf(" ");
		}
		for (x = 0; x < 2 * i + 1; x++)
		{
			printf("*");
		}
		printf("\n");
	}
	n = 1;
	for (i = 3; i >0; i--)
	{
		for (j = 0; j <=n; j++)
		{
			printf(" ");
		}
		for (x = 2 * (i-1) - 1; x > 0; x--)
		{
			printf("*");
		}
		n++;
		printf("\n");
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值