这个试下吧,微软命令行编译器编译通过,运行正常;BCC55编译通过,运行有点问题;VC6 IDE下编译通过,运行出错。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
void flushstdin()
{
char c;
while((c=getchar())!='\n'&&c!=EOF);
}
struct strs
{
char *thestr;
struct strs *next;
};
int main()
{
int i=0;
char x,y;
struct strs *head=NULL,*current=NULL,*new=NULL,*temp=NULL;
char *str1="";
while(1)
{
printf("please input string:\n");
scanf("%s",str1);
if(!strcmp(str1,"exit")) return 0;
if(strcmp(str1,"output")==0)
{
current=head;
while(current!=NULL)
{
printf("%s\n",current->thestr);
temp=current;
current=current->next;
}
str1=(char *)malloc(sizeof(char *));
current=temp;
continue;
}
if(strcmp(str1,"replace")==0)
{
printf("请输入替换的字母与替换后的字母\n");
flushstdin();
scanf("%c%c",&x,&y);
current=head;
while(current!=NULL)
{
i=0;
while(current->thestr[i]!='\0')
{
if(current->thestr[i]==x)
current->thestr[i]=y;
i++;
}
printf("%s\n",current->thestr);
temp=current;
current=current->next;
}
str1=(char *)malloc(sizeof(char *));
current=temp;
continue;
}
i=0;
while(str1[i]!='\0')
{
if(str1[i]>='a'&&str1[i]<='z')
str1[i]=(str1[i]-'a'+3)%26+'a';
printf("%c",str1[i]);
i++;
}
printf("\n");
new=(struct strs *)malloc(sizeof(struct strs));
if(head==NULL)
{
head=new;
current=new;
}
else
{
current->next=new;
current=new;
}
current->thestr=str1;
str1=(char *)malloc(sizeof(char *));
}
return 0;
}