POJ过滤多余的空格

版权声明:@ly https://blog.csdn.net/lytwy123/article/details/83548095

1.题目描述:


2.算法分析:

首先,要过滤掉多余的空格,我们如果一个一个删除空格是一件很麻烦的事,不妨我们可以从每个单词的后面加一个空格,这样是不是会比把多余空格删除好一些呢。怎么做?

一个知识点大家要了解,字符串的输入方式有两种

//1.scanf是从光标开始的地方读到空格就结束了,也就相当于读一个单词
scanf("%s",&str);
//2.gets()是从光标开始的地方读到换行,包含空格也读进去了
gets(str);

这一道题目我们可以使用scanf只读入单词,在输出的时候使用printf加入空格即可。


3.源代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

char str[201];

int main()
{
	while(scanf("%s",&str)==1)   //循环读入数据,在读不到的时候停止循环 
	printf("%s ",str);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lytwy123/article/details/83548095