除去空格,使句子完美

来源:互联网 发布:tsp 组合优化问题 编辑:程序博客网 时间:2024/06/06 01:41
/* * Copyright (c) 2012, 烟台大学计算机学院                     * All rights reserved.                     * 文件名称:test.cpp                     * 作者:王至超                  * 完成日期:2013 年12月14日                     * 版本号:v1.0                   *                     * 输入描述:无                     * 问题描述:出去字符串中多余的空格                  * 程序输出:整理好的句子* 问题分析:                    * 算法设计:略                     */         #include<iostream>using namespace std;int main(){char str[]="Only    one   space is    allowed    between  the  two             words.";cout<<"原始难看的句子:"<<str<<endl;int i=0,j=0;bool notSpace;while(str[j]==' ')j++;notSpace=true;while(str[j]!='\0'){if(str[j]!=' '){notSpace=true;str[i++]=str[j++];}else if(notSpace){notSpace=false;str[i++]=str[j++];}else{j++;}}str[i]='\0';cout<<"整理后的句子:"<<str<<endl;return 0;}

0 0
原创粉丝点击