YTU 2720: 删出多余的空格

来源:互联网 发布:linux的dd命令详解 编辑:程序博客网 时间:2024/05/21 18:43

Description

小平在给弟弟检查英语作业时时,发现每个英语句子单词之间的空格个数不等,请你编程帮他把句子中多余的空格去掉,使每个单词之间只保留一个空格,首尾的空格也去掉。
部分代码已给定如下,只需要提交缺失的代码。

#include <string.h>
int main()
{
    void delSpace(char sentence[]);
    char sentence[1000];
    gets(sentence);
    delSpace(sentence);
    puts(sentence);
    return 0;
}

Input

英文句子

Output

删除多余空格后的句子

Sample Input

   Happiness     is   a way     station between    too much    and too    little.     

Sample Output

Happiness is a way station between too much and too little.

HINT

Source

lyh

ac代码:

#include <string.h>#include <stdio.h>#include<iostream>using namespace std;int main(){    void delSpace(char sentence[]);    char sentence[1000];    gets(sentence);    delSpace(sentence);    puts(sentence);    return 0;}void delSpace(char sentence[]){    int len=strlen(sentence);    char t[len];    int i=0,j=0;    while(i){        if(sentence[i]!=' ')            break;        i++;    };    while(i<len){      while(sentence[i++]!=' '){           t[j++]=sentence[i-1];      }      if(sentence[i]!=' ')        t[j++]=' ';    }    t[j-1]='\0';    for(i=0;i<j;i++)        t[i]=t[i+1];    strcpy(sentence,t);}

运行结果:


0 0
原创粉丝点击