当前位置:网站首页>P: Filter extra spaces

P: Filter extra spaces

2020-12-06 00:21:44 Encourage

The time limit : 
1000ms
 
Memory limit : 
65536kB
describe

There may be multiple consecutive spaces in a sentence , Filter out the extra space , Just leave a space .

Input
a line , A string ( Length not exceeding 200), There are no spaces at the beginning and the end of a sentence .
Output
Filtered sentences .
The sample input
Hello      world.This is    c language.
Sample output
Hello world.This is c language.
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char a[500],i;
 6     gets(a);
 7     for(i=0;i<strlen(a);i++)
 8     {
 9         if(a[i]!=' '&&a[i]!='\0') printf("%c",a[i]);
10         else if(a[i]==' '&&a[i+1]!=' ') printf(" ");
11     }
12     return 0;
13 }

 

 1 // There will be pe
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     string s;
 8     getline(cin, s);
 9     int pos;
10     for (int i=0;i<s.length();++i)
11     {   
12         if ((pos = s.find("  ")) != string::npos)
13         {
14             s.replace(pos, 2, " ");
15         }
16     }
17     cout << s;
18     return 0;
19 }

 

版权声明
本文为[Encourage]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201206002004164u.html