|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Aseeker 于 2020-2-10 15:03 编辑
#include <string.h>
#include <stdio.h>
void fun( char s[],int c)
{
int i,j,len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]==c)
{
for(j=i;j<len;j++)
{
s[j]=s[j+1];
}
}
s[j]='\0';//为什么在这里的j是未赋值,而不是等于len ,我知道这句不用加,没有用
}
}
void main()
{
static char str[]="turbo c and borland c++";
char ch;
FILE *out;
printf ("原始字符串:%s\n ",str);
printf("输入一个字符串:\n");
scanf("%c",&ch);
fun(str,ch);
printf("str[]=%s\n",str);
strcpy(str,"turbo c and borland c++");
fun(str,'a');
/******************************/
out=fopen("out.dat","w");
fprintf(out,"%s",str);
fclose(out);
/******************************/
system("pause");
}
原因为:
1. j在定义阶段没有初始化值
2. 条件if(s==c) 没有成立,导致j没有赋值。
|
|