编写函数output和input,其功能分别与gets和puts相同,函数中用getchar和putchar

要求用指针完成程序

1、char *input(char *a)

{

int i=0;

while ((a[i]=getchar())!='\n')

i++;

a[i]='\0';

return a;

}

int output(const char *a)

{

int i;

for (i = 0; a[i]; i++) {

if (putchar(a[i])==EOF) return EOF;

}

putchar('\n');

return i;

}

2、//编写函数output和input,其功能分别于gets和puts相同,函数中用getchar和purchar读入和输出字符 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>//包含getch()函数

 //系统的gets()函数没有判断输入字符个数是否超过接收缓冲区buf的大小.

//同样,我们这里也不判断

char *input(char *buf)

{

char c = 0;

char *pSaveWork = buf;

while ( true )

{

fflush(stdin);

c = getch();

扩展资料:

如果没有返回值类型名为"void", 整数类型int 类型返回值为整数类型int,以此类推……

类型名有:void int long float int* long* float* ……

C++中函数的调用:函数必须声明后才可以被调用。调用格式为:函数名(实参)

调用时函数名后的小括号中的实参必须和声明函数时的函数括号中的形参个数相同。

有返回值的函数可以进行计算,也可以做为右值进行赋值。

参考资料来源:百度百科-函数

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-07-31
//编写函数output和input,其功能分别于gets和puts相同,函数中用getchar和purchar读入和输出字符 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>//包含getch()函数

//系统的gets()函数没有判断输入字符个数是否超过接收缓冲区buf的大小.
//同样,我们这里也不判断
char *input(char *buf)
{
char c = 0;

char *pSaveWork = buf;
while ( true )
{
fflush(stdin);
c = getch();
if ( c == '\r' || c == '\n' )
{
putc('\n',stdout);
fflush(stdout);
break;
}
putc(c,stdout);
fflush(stdout);
*pSaveWork = c;
pSaveWork++;
}
*pSaveWork = 0x0;
return buf;
}

int output(const char *str)
{
if ( NULL == str )
{
return -1;
}
int rtv = strlen(str);
char *pWork = (char *)str;
while( *pWork != '\0')
{
putchar(*pWork);
pWork++;
}
return rtv;
}

int main(void)
{
char str[100];
memset(str,0x0,sizeof(str));
input(str);
output(str);

getchar();
return 0;
}

第2个回答  2010-06-01
char *input(char *a)
{

int i=0;
while ((a[i]=getchar())!='\n')
i++;

a[i]='\0';

return a;

}

int output(const char *a)
{
int i;
for (i = 0; a[i]; i++) {
if (putchar(a[i])==EOF) return EOF;
}
putchar('\n');
return i;
}本回答被提问者采纳
相似回答