7月7日作业 DAY7

这篇文章包含多个C语言代码示例,分别实现了打印杨辉三角、输出字母矩形、找出特定条件的凶手、检查数字的因数、字符串反转、将字符串转为整数、删除字符串中的空格、计算字符串长度、拷贝字符串、连接字符串以及比较字符串。这些代码展示了基础的算法和字符串处理技巧。
摘要由CSDN通过智能技术生成

 1.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int n;
	printf("请输入行列:\n");
	scanf("%d",&n);
	int i, j, arr[n][n];
	printf("打印%d行杨辉三角\n", n);
	for(i = 0; i < n; i++)
	{
		for (j = 0; j < n - i -1; j++)
		{
			printf(" ");
		}
		for(j = 0; j <= i; j++)
		{
			if( j == 0 || i == j)
			{
				arr[i][j] = 1;
			}
			else
			{
				arr[i][j]=arr[i - 1][j - 1] + arr[i - 1][j];
			}
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}
	return 0;
}

结果

2.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char n;
	printf("请输入大写字母:\n");
	n = getchar();
	for(char i = 'A';i <= n;i++)
	{
		for(char j = 'A';j <= i;j++)
		{
			printf("%c", i);
		}
		printf("\n");
	}
	printf("\n");

	for(char i = n;i >= 'A';i--)
	{
		for(char j = n;j >= n - i + 'A';j--)
		{
			printf("%c", i);
		}
		printf("\n");
	}
	return 0;
}

结果

3.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char killer;
	for(killer = 'A'; killer <= 'D'; killer++)
	{
		if((killer != 'A') + 2 * (killer != 'B' && killer == 'C') + (killer == 'A' || killer == 'D') == 2)
			printf("凶手是%c\n", killer);
	}
	return 0;
}

 结果

 4.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int num;
	printf("请输入一个整数:\n");
	scanf("%d", &num);
	if(num % 3 == 0 && num % 5 == 0 && num % 7 == 0)
	{
		printf ("3 5 7\n");
	}
	else if(num % 3 == 0 && num % 5 == 0)
	{
		printf ("3 5 \n");
	}
	else if(num % 3 == 0 && num % 7 == 0)
	{
		printf ("3 7 \n");
	}
	else if(num % 7 == 0 && num % 5 == 0)
	{
		printf ("5 7 \n");
	}
	else if(num % 3 == 0)
	{
		printf ("3\n");
	}
	else if(num % 5 == 0)
	{
		printf ("5\n");
	}
	else if(num % 7 == 0)
	{
		printf ("7\n");
	}
	else
	{
		printf ("n\n");
	}
	return 0;
}

结果

5.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int i = 0;
	int j;
	char str[100];
	gets(str);
	j = strlen(str) - 1;
	while(i < j)
	{
		char t = str[i];
		str[i] = str[j];
		str[j] = t;
		i++;
		j--;
	}
	puts(str);
	i = 0;
	j = 0;
	while(str[i] != '\0')
	{
		while (str[j] != ' ' && str[j] != '\0')
		{
			j++;
		}
		int k = j - 1;
		while(i < k)
		{
			char t = str[i];
			str[i] = str[k];
			str[k] = t;
			i++;
			k--;
		}
		while(str[j] == ' ')
		{
			j++;
		}
		i = j;
	}
	puts(str);
	return 0;
}

结果

6.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char str[100];
	gets(str);
	int i = 0;
	int sum = 0;
	while (str[i])
	{
		int n = str[i] - '0';
		sum = sum *10 +n;
		i++;
	}
	printf("转换成整数数字为:\n");
	printf("%d\n", sum);
	return 0;
}

 结果

7.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int i = 0, j = 0;
	char str[100];
	printf("请输入字符串:\n");
	gets(str);
	for (i = 0; i < strlen(str); i++)
	{
		if(str[i] != ' ')
			str[j++] = str[i];
	}
	str[j] = '\0';
	printf("字符串删除空格为:\n");
	puts(str);
	return 0;
}

 结果

8.代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int count = 0;
	char str1[100];
	int i;
	int j;
	printf("计算字符串长度:\n");
	printf("请输入字符串:\n");
	gets(str1);
	for(i = 0; str1[i] != '\0'; i++)
		count++;
	printf("字符串长度为:%d\n", count);	
	str1[0] = 0;
	printf("\n");

	printf("拷贝字符串:\n");
	char str2[100];
	printf("请输入第一个字符串:\n");
	gets(str1);
	printf("请输入第二个字符串:\n");
	gets(str2);
	for(i = 0; str2[i]; i++)
		str1[i] = str2[i];
	str1[i] = 0;
	printf("str1 = %s, str2 = %s\n", str1, str2);
	str1[0] = 0;
	str2[0] = 0;
	printf("\n");

	printf("连接字符串:\n");
	printf("请输入第一个字符串:\n");
	gets(str1);
	printf("请输入第二个字符串:\n");
	gets(str2);
	for(i = 0; str1[i]; i++);
	for(j = 0; str2[j]; j++)
	{
		str1[i] = str2[j];
		i++;
	}
	str1[i] = 0;
	printf("str1 = %s, str2 = %s\n", str1, str2);
	str1[0] = 0;
	str2[0] = 0;
	i = 0;
	j = 0;
	printf("\n");

	printf("比较字符串:\n");
	printf("请输入第一个字符串:\n");
	gets(str1);
	printf("请输入第二个字符串:\n");
	gets(str2);
	while(str1[i] == str2[i])
	{
		if(str1[i] == 0)
			break;
		i++;
	}
if(str1[i] > str2[j])
    printf("str1>str2\n");
else if(str1[i]<str2[j])
    printf("str1 < str2\n");
else
    printf("str1 = str2\b");
	return 0;
}

 结果

思维导图

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值