当前位置:编程学习 > C/C++ >>

除去字符串中多余的空格(第十六周上机任务)

[cpp]  
/*  
* Copyright (c) 2012, 烟台大学计算机学院                      
* All rights reserved.                      
* 文件名称:test.cpp                      
* 作者:樊露露                     
* 完成日期:2012 年12月16日                      
* 版本号: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;  
}  
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,