方法1:
void fun(char* s, char* t)
{
char* q = s;
int i = 0, j = 0, k = 0;
for (q; q < t; q++)
{
if (*q != '*')
{
s[i++] = *q;
}
k = i;
}
for (q; *q != '\0'; q++)
{
s[k++] = *q;
j = k;
}
s[j] = '\0';
}
int main()
{
char s[81], * t;
printf("请输入字符串\n");
gets(s);
printf("初始字符串为%s\n", s);
t = s;
while (*t)
{
t++;
}
t--;
while (*t == '*')//直到出现最后一个字母
{
t--;
}
fun(s, t);
printf("经过删减的字符串为%s", s);
return 0;
}
方法2:
该方法是通过文本文件的读写和输出
void fun(char* s, char* t)
{
char* q;
int i = 0, j = 0, k = 0;
for (q = s; q < t; q++)
{
if (*q != '*')
{
s[i++] = *q;
}
k = i;
}
for (q; *q != '\0'; q++)
{
s[k++] = *q;
j = k;
}
s[j] = '\0';
}
int main()
{
char s[200], * t;
int i;
FILE* in = fopen("in.txt", "r");
FILE* out = fopen("out.txt", "w");
if (in == NULL || out == NULL)
{
perror("error");
exit(1);
}
for (i = 0; i < 3; i ++ )
{
fscanf(in, "%s", s);
t = s;
while (*t)
{
t++;
}
t--;
while (*t == '*')
{
t--;
}
fun(s, t);
//修改好的s代码写入到out.txt中
fprintf(out, "%s\n",s);
}
fclose(in);
fclose(out);
return 0;
}