10.19
//第十九题
#include <stdio.h>
char* mygets(char* buf);
int myputs(char* cstr);
main()
{
char s[99];
printf("请输入字符串:");
mygets(s);
printf("输出字符串:");
myputs(s);
}
char* mygets(char *buf)
{
int c;
char *s;
for (s = buf; (c = getchar()) != '\n';)
if (c == EOF)
if (s == buf)
return (NULL);
else
break;
else
*s++ = c;
*s = '\0';
return (buf);
}
int myputs(char* cstr)
{
int i = 0;
while ('\0' != cstr[i++])
{
putchar(cstr[i-1]);
}
putchar('\n');
return i;
}
10.20
//第二十题
#include <stdio.h>
int fun(char* s);
main()
{
char str[99];
puts("请输入一字符串:");
gets(str);
puts("输出该字符串:");
puts(str);
if (1 == fun(str))
{
printf("%s 是回文。\n", str);
}
else
{
printf("%s 不是回文。\n", str);
}
}
//判断字符串是否是回文
int fun(char* s)
{
int i = 0;
int j = strlen(s)-1;
while (i<j)
{
if (s[i++] != s[j--])
{
return 0;
}
}
return 1;
}
10.21
//第二十一题
#include <stdio.h>
char del_char(char* str, int i);
main()
{
char s[99];
int i;
char c;
printf("请输入一串字符:");
gets(s);
printf("输出该串字符:");
puts(s);
printf("输入一整数:");
scanf("%d", &i);
if (NULL != (c = del_char(s, i)))
{
printf("删除第%d个字符成功,删除后的字符串为:%s\n", i, s);
printf("删除的字符为:%c", c);
}
else
{
printf("删除不成功。\n");
}
}
//删除字符串中的第i个元素
char del_char(char* str, int i)
{
char ch;
if (i < 0 || i >= strlen(str))
{
return NULL;
}
else
{
ch = str[i];
while (i < strlen(str))
{
str[i-1] = str[i];
++i;
}
str[i-1] = '\0';
return ch;
}
}