#include
#include
void deletechar(char *str,char n)
{
char *head=str;
char *move=str;
while(move)
{
if( *move != n )
{
*head=*move;
head++;
}
move++;
}
*str='\0';
printf("%s",head);
}
int main()
{
printf("请从键盘中输入一个字符串\n");
char arr[10];
char c;
gets(arr);
printf("请输入要删除的字符\n");
scanf("%c",&c);
printf("删除后的字符串为:\n");
deletechar(arr,c);
system("pause");
return 0;
}
删除字符串中指定元素,此代码哪里出错了呀?
供参考:
#include<stdio.h>
#include<stdlib.h>
void deletechar(char *str,char n)
{
char *head=str;
char *move=str;
while(*move) //while(move)
{
if(*move != n)
{
*head=*move;
head++;
}
move++;
}
*head='\0'; //*str='\0';
printf("%s",str); //printf("%s",head);
}
int main()
{
printf("请从键盘中输入一个字符串\n");
char arr[10];
char c;
gets(arr);
printf("请输入要删除的字符\n");
scanf("%c",&c);
printf("删除后的字符串为:\n");
deletechar(arr,c);
system("pause");
return 0;
}
这 * str='\0'; 这肯定是错误的。函数中str指针根本就没动过,你这样等于将字符串清空了啊
应该是*head = '\0'啊