关于’\0‘,
#include<stdio.h>void delchar(char *s,char c)
{
char *p;
for(p=s;*s;s++)
if(*s!=c)
{
*p++=*s;
}
}
void main()
{
char c,s[100];
printf("Input a string:\n");
gets(s);
printf("Input a char:After deleted,the string is:\n");
scanf("%c",&c);
delchar(s,c);
printf("%s\n",s);
}
#include<stdio.h>
void delchar(char *s,char c)
{
char *p;
for(p=s;*s;s++)
if(*s!=c)
{
*p++=*s;
}
*p='\0'
}
void main()
{
char c,s[100];
printf("Input a string:\n");
gets(s);
printf("Input a char:After deleted,the string is:\n");
scanf("%c",&c);
delchar(s,c);
printf("%s\n",s);
}
按第一个代码输入happy new year!
输出的是hppy new yer!
按第二个代码输出的却是 hppy new yer!r!
请问这是为什么?
[此贴子已经被作者于2017-12-24 12:08编辑过]