C语言基础练习5


1.求一个3X3整型矩阵对角线元素之和

#include<stdio.h>

int main()
{
	int a[3][3] = { { 1,2,3 },{ 4,5,6 },{ 7,8,9 } };
	int i, j, sum = 0;
	for (i = 0; i<3; i++)
	{
		for (j = 0; j<3; j++)
		{
			if (i == j || i + j == 2)
			{
				sum += a[i][j];
			}
		}
	}
	printf("%d\n", sum);
    return 0;
}

2.有一篇文章,共有3行文字,每行有80个字符。分别统计出大写字母、小写字母、数字、空格以及其他字符的个数

#include<stdio.h>
#include<string.h>

int main()
{
	int capitalletter = 0, lowerletter = 0, space = 0, number = 0, other = 0, i, j;
	char text[3][80];
	for (i = 0; i<3; i++)
	{
		printf("please input line %d:\n", i + 1);
		gets_s(text[i]);
		for (j = 0; j<80 && text[i][j] != '\0'; j++)
		{
			if (text[i][j] >= 'A'&& text[i][j] <= 'Z')
				capitalletter++;
			else if (text[i][j] >= 'a' && text[i][j] <= 'z')
				lowerletter++;
			else if (text[i][j] >= '0' && text[i][j] <= '9')
				number++;
			else if (text[i][j] == ' ')
				space++;
			else
				other++;
		}
	}
	printf("capitalletter=%d\nlowerletter=%d\nspace=%d\nnumber=%d\nother=%d\n", capitalletter, lowerletter,space, number, other);
    return 0;
}

3.编一程序,将两个字符串连接起来,不用strcat函数

#include<stdio.h>
#include<string.h>

int main()
{
	char c1[80], c2[80];
	int i, j;
	printf("Input string1:");
	gets_s(c1);
	printf("Input string2:");
	gets_s(c2);
	for (j = 0, i = strlen(c1); c2[j] != '\0'; i++, j++)
		c1[i] = c2[j];
	c1[i] = '\0';
	puts(c1);
    return 0;
}



猜你喜欢

转载自blog.csdn.net/huaweiran1993/article/details/78303957