C语言基础练习15


1.输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换

#include "stdafx.h"
#include


int main()
{
	void input(int *);
	void swap(int *);
	void output(int *);
	int num[10];
	input(num);
	swap(num);
	output(num);
    return 0;
}

void input(int *num)
{
	int i;
	printf("input 10 numbers:");
	for (i = 0; i < 10; i++)
		scanf_s("%d", &num[i]);
}

void swap(int *num)
{
	int *max, *min, *p, temp;
	max = min = num;
	for (p = num + 1; p < num + 10; p++)
		if (*p > *max)max = p;
		else if (*p < *min)min = p;
		temp = num[0]; num[0] = *min; *min = temp;
		if (max == num)max = min;
		temp = num[9]; num[9] = *max; *max = temp;
}

void output(int *num)
{
	int *p;
	printf("Now,they are:\n");
	for (p = num; p < num + 10; p++)
		printf("%d ", *p);
	printf("\n");
}
运行结果:

C语言基础练习15_第1张图片


2.写一个函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度

#include "stdafx.h"
#include

int main()
{
	int length(char *p);
	int i,len;
	char str[20];
	printf("input string:");
	scanf_s("%s", str,20);
	len = length(str);
	printf("The length of string is %d.\n", len);
    return 0;
}

int length(char *p)
{
	int n;
	n = 0;
	while (*p != '\0')
	{
		n++; p++;
	}
	return(n);
}
运行结果:

C语言基础练习15_第2张图片


3.编一程序,输入月份号,输出该月的英文月名

#include "stdafx.h"
#include

int main()
{
	char *month[12] = { "January","February","March","April",
		"May","June","July","August","September","October","November","December" };
	int n;
	printf("input month:\n");
	scanf_s("%d", &n);
	if ((n <= 12) && (n >= 1))
		printf("It is %s.\n", *(month + n-1));
	else printf("It is a wrong number.\n");
    return 0;
}
运行结果:

C语言基础练习15_第3张图片


你可能感兴趣的:(c语言)