C语言输出字符串
C语言标准函数库中 printf 函数和 puts 函数都可以输出字符串,但各有优点和缺点。我们综合两者的优点,设计一个函数来输出字符串。
函数原型
int PutStr(const char *str);
说明:str 为字符串的起始地址。函数将输出 str 所指示的字符串,不自动换行。函数值为输出字符的数目。
裁判程序
#include <stdio.h>
int PutStr(const char *str);
int main()
{
char a[1024];
int n;
gets(a);
n = PutStr(a);
printf("(%d)\n", n);
return 0;
}
/* 你提交的代码将被嵌在这里 */
输入样例
John
输出样例
John(4)
提交答案:
int PutStr(const char *str)
{
char *p = str;
while(*p++);
for(int i = 0; str[i] != '\0'; i++)
{
printf("%c", str[i]);
}
return p - str - 1;
}