C语言两道编程题
(1)编程实现凯撒加密法,将原来的小写字母用后三个字母的大写形式替换,x,y,z替换为a,b,c,大写字母同理。
要求:
(1)要在主函数里初始化字符串为"To Be successful, Be crazy first."
(2)写一个void kaisa(charstr)函数供主函数调用,用来对主函数里的字符串加密;
(3)在主函数里显示输出加密后的字符串。*
/*第四题第一小题*/
#include<stdio.h>
void kaisa(char* str2);
int main()
{
int i=0,y=0;
char str1[] = { "To Be successful, Be crazy first." }, * str2;
str2 = str1;
kaisa(str1);
while (str1[y] != '\0')
{
printf("%c", str1[y]);
y++;
}
}
void kaisa(char* str1)
{
int x=0;
for (x = 0; str1[x] != '\0'; x++)
{
if (str1[x] >= 'a' && str1[x] <= 'w')
{
str1[x] = str1[x] - 29;
continue;
}
if (str1[x] >= 'A' && str1[x] <= 'W')
{
str1[x] = str1[x] + 35;
continue;
}
if (str1[x] == 'y' || str1[x] == 'z' || str1[x] == 'x')
{
str1[x] = str1[x] - 55;
continue;
}
if (str1[x] == 'Y' || str1[x] == 'Z' || str1[x] == 'X')
{
str1[x] = str1[x] + 9;
continue;
}
}
}
注意事项:
进行函数调用时,数据是从实参kaisa(str1)传到形参kaisa(char* str1),此题中,str1的数据被覆盖了。
(2)从主函数随机输入十个整数,调用函数找最小数,并在主函数输出最小值。
/*第四题第二小问*/
#include <stdio.h>
int xiao(int x,int a[10]);
int main()
{
int a[10], i, y=0;
printf("请输入十个整数\n");
for (i = 0; i < 10; i++)
scanf_s("%d", &a[i]);
printf("最小值为%d\n", xiao(y,a));
return 0;
}
int xiao(int x,int a[10])
{
int k,t;
a[10];
k = a[0];
for (t = 0; t < 10; t++)
if (k > a[t])
k = a[t];
return k;
}
注意事项:形参与实参的定义必须一样。