沈阳师范大学大一上册C语言PTA题目集以及答案(第六章 指针 函数题篇)
6-1 字符串的连接 (15分)
本题要求实现一个函数,将两个字符串连接起来。
函数接口定义:
char *str_cat( char *s, char *t );
函数str_cat应将字符串t复制到字符串s的末端,并且返回字符串s的首地址。
裁判测试程序样例:
#include <stdio.h>
#include <string.h>
#define MAXS 10
char *str_cat( char *s, char *t );
int main()
{
char *p;
char str1[MAXS+MAXS] = {’\0’}, str2[MAXS] = {’\0’};
scanf("%s%s", str1, str2);
p = str_cat(str1, str2);
printf("%s\n%s\n", p, str1);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
abc
def
输出样例:
abcdef
abcdef
char *str_cat( char *s, char *t )
{
strcat(s,t);
return s;
}
6-2 利用指针找最大值 (10分)
本题要求实现一个简单函数,找出两个数中的最大值。
函数接口定义:
void findmax( int *px, int *py, int *pmax );
其中px和py是用户传入的两个整数的指针。函数findmax应找出两个指针所指向的整数中的最大值,存放在pmax指向的位置。
裁判测试程序样例:
#include <stdio.h>
void findmax( int *px, int *py, int *pmax );
int main()
{
int max, x, y;
scanf("%d %d", &x, &y);
findmax( &x, &y, &max );
printf("%d\n", max);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3 5
输出样例:
5
void findmax( int *px, int *py, int *pmax )
{
if(*px<*py)
*px=*py;
*pmax=*px;
//return pmax;
}
6-3 删除字符串中数字字符 (10分)
删除一个字符串中的所有数字字符。
函数接口定义:
void delnum(char *s);
其中 s 是用户传入的参数。 函数的功能是删除指针 s 所指的字符串中的所有数字字符。
裁判测试程序样例:
#include “stdio.h”
void delnum(char *s);
int main ()
{ char item[80];
gets(item);
delnum(item);
printf("%s\n",item);
return 0;
}
/* 请在这里填写答案 */
输入样例:
a0bc+d496df
输出样例:
abc+ddf
void delnum(char *s)
{
int i=0,j=0,k=0;
char b[100];
while(s[i]!='\0')
{if(s[i]>='0'&&s[i]<='9')
s[i]='\0';
i++;
}
for(k=0;k<=i;k++)
{
if(s[k]!='\0')
{
b[j]=s[k];
j++;
}
}
for(k=0;k<=i;k++)
{
s[k]=b[k];
}
//*s=b[100];
//puts(b);
}