问题5_1
函数
f
u
n
fun
fun的功能是:将形参
n
n
n 中,各位上为偶数的数取出,并按原来从高位到低位相反的顺序组成一个新数,作为函数值返回。
例如,输入一个整数
27638496
27638496
27638496,则函数返回值为
64862
64862
64862。
代码5_1
#include<stdio.h>
unsigned long fun(unsigned long n){
unsigned long x = 0;
int t;
while(n){
t = n%10;
if(t%2 == 0)
x = x*10+t;
n = n/10;
}
return x;
}
void main(void){
unsigned long n = -1;
while(n>99999999||n<0){
printf("Please input (0<n<100000000):");
scanf("%ld", &n);
}
printf("\n The result is:%ld\n", fun(n));
}
结果5_1
问题5_2
函数
f
u
n
fun
fun的功能是:将长整型数中各位上为奇数的数依次取出,构成一个新数放在
t
t
t 中,高位仍在高位,低位仍在低位。
例如,若形参
s
s
s 中的数为
87653142
87653142
87653142,则
t
t
t 中的数为
7531
7531
7531。
代码5_2
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void fun(long s, long* t){
int d;
long s1 = 1;
*t = 0;
while(s>0){
d = s%10;
if(d%2!=0){
*t = d*s1 + *t;
s1 *= 10;
}
s /= 10;
}
}
void main(void){
long s, t;
system("CLS");
printf("\n Please enter s:\n");
scanf("%ld", &s);
fun(s, &t);
printf("The result is:%ld\n", t);
}
结果5_2
问题5_3
函数
f
u
n
fun
fun的功能是:实现两个字符串的连接(不要使用库函数
s
t
r
c
a
t
strcat
strcat),即把
p
2
p2
p2 所指的字符串连接到
p
1
p1
p1 所指的字符串后面。
例如,分别输入下面两个字符串:
"
Z
h
i
y
i
S
t
r
i
n
g
−
−
"
"ZhiyiString--"
"ZhiyiString−−"
"
S
h
o
u
d
a
o
S
t
r
i
n
g
"
"ShoudaoString"
"ShoudaoString"
程序输出:
"
Z
h
i
y
i
S
t
r
i
n
g
−
−
S
h
o
u
d
a
o
S
t
r
i
n
g
"
"ZhiyiString--ShoudaoString"
"ZhiyiString−−ShoudaoString"
代码5_3
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void fun(char p1[], char p2[]){
int i, j;
for(i=0; p1[i]!='\0'; i++); // 求出的 i 为 p1 字符串的总长度,包括结束标记符
for(j=0; p2[j]!='\0'; j++)
p1[i++] = p2[j]; // 将 p2 字符串连接在 p1 字符串后面
p1[i] = '\0'; // 在字符串最后加上结束标记符
}
void main(void){
char s1[80], s2[40];
system("CLS");
printf("Enter s1 and s2:\n");
scanf("%s %s", s1, s2);
printf("s1 = %s\n", s1);
printf("s2 = %s\n", s2);
printf("Invoke fun(s1, s2):\n");
fun(s1, s2);
printf("After invoking:\n");
printf("%s\n", s1);
}