每日一题4.10.1

每日一题4.10.1

收件人列表

在这里插入图片描述
在这里插入图片描述
解题思路: 输入字符串,判断内部是否有空格或逗号,用双引号包含
代码实现:

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
	int n;
	while (cin >> n)
	{
		cin.get();
		string name;
		for (int i = 0; i < n; i++)
		{
			bool quote = false;
			getline(cin, name);
			if (name.find(',') != string::npos || name.find(' ') != string::npos)
			{
				quote = true;
			}
			if (quote)
			{
				printf("\"");
			}
			printf("%s", name.c_str());
			if (quote)
			{
				printf("\"");
			}
			if (i == n - 1)
			{
				printf("\n");
			}
			else
			{
				printf(", "); 
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lxb18821659801/article/details/89304735