爱问知识人 爱问教育 医院库

怎么才能输出空格字符?

首页

怎么才能输出空格字符?

这个字符串编辑程序是用单链表做的,程序最后要求输出编辑后的字符串,比如执行替换操作,把o换成e,则原字符串Thisisabook.变为Thisisabeek.但是现在我写的这个程序输出的是Thisisabeek.单词中间没有空格。怎样才能把空格加上呢?#include<iostream.h>#include<malloc.h>typedefstructnode{chardata;structnode*next;}node,*Lstring;//结构体定义voidcreat(Lstring&s){node*p1,*p2;s=(Lstring)malloc(sizeof(node));//新建头结点cout<<"请输入字符串:";p2=s;p1=p2;while(p1->data!='.')//建立链表{p1=(Lstring)malloc(sizeof(node));//新建首元结点cin>>p1->data;p2->next=p1;p2=p2->next;}p1->next=NULL;}voidinsert(Lstring&s)//插入函数{node*p1,*p2,*q;charc,x;repeat:;cout<<"需要在哪个字符之前插入?";cin>>c;for(p1=s,p2=p1;p1->next!=NULL;p1=p1->next)if(p1->next->data==c)p2=p1;if(p2==s){cout<<endl<<"字符串中不存在字符"<<c<<",请重新输入!"<<endl<<endl;gotorepeat;}cout<<"请输入欲插入的字符:";cin>>x;q=(Lstring)malloc(sizeof(node));q->data=x;q->next=p2->next;p2->next=q;}voiddel(Lstring&s){node*p,*q;charc;repeat:;cout<<"请输入欲删除的字符:";cin>>c;p=s;while(p->next->data!=c){p=p->next;if(p->next==NULL){cout<<endl<<"字符串中不存在字符"<<c<<",请重新输入!"<<endl<<endl;gotorepeat;}}q=p->next;p->next=q->next;free(q);}voidtrans(Lstring&s){node*p;chara1,a2;inti=0;cout<<"请输入被替换字符:";cin>>a1;cout<<"请输入替换的字符:";cin>>a2;p=s;while(p->next!=NULL){if(p->next->data==a1){p->next->data=a2;++i;}p=p->next;}if(i==0){cout<<endl<<"字符串中不存在字符"<<a1<<",请重新输入!"<<endl<<endl;trans(s);}}voidmain(){repeat2:;structnode*p;Lstringa;creat(a);cout<<endl<<"您可以输入这3个指令之一:I—插入D—删除R—替换"<<endl;repeat1:;cout<<endl<<"请输入指令:";chary;cin>>y;if(y!='I'&&y!='D'&&y!='R'){cout<<endl<<"您输入了一个错误指令,请重新输入!"<<endl;gotorepeat1;}switch(y){case'I':insert(a);break;case'D':del(a);break;case'R':trans(a);break; }cout<<endl<<"结果是:";p=a->next;if(p==NULL)cout<<"空串"<<endl;else{while(p!=NULL){cout<data;p=p->next;}}rep:;cout<<endl<<endl<<"是否继续编辑当前字符串?(y/n)";cin>>y;if(y=='y')gotorepeat1;elseif(y=='n')cout<<endl<<"是否重新输入字符串?(y/n)";else{cout<<endl<<"请按Y或N进行选择!";gotorep;}cin>>y;if(y=='y'){cout<<endl<<endl;gotorepeat2;}}

提交回答

全部答案

    2005-10-27 19:35:48
  • 源码错误不少。略一看,发现问题,(不包括风格的问题)
    在创建链表时,输入字符cin的方法不对。会过滤空格,作为下一次输入的标志。可以查一下,创建之后输出来应该也没有空格符。
    while(p1->data!='.') //建立链表
    {
    p1=(Lstring)malloc(sizeof(node)); //新建首元结点
    cin>>p1->data;
    }
    

    d***

    2005-10-27 19:35:48

类似问题

换一换
  • C/C++ 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 176-195
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):