C++同一行輸入多個字符串,以回車結束輸入

http://noi.openjudge.cn/ch0107/21/

這裏需要對輸入進行控制,第一行輸入多個字符串後,以回車結束,使用了getchar()來存儲輸入的字符,並和換行符'\n'比較。

//http://noi.openjudge.cn/ch0107/21/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
	string s[100], a, b;
	char ch;
	
	int i=0;
	
	//一行輸入字符數組,遇到回車結束輸入 
	while(cin >> s[i])
        {
    	    i++;
    	    if(ch=getchar()=='\n')
    		break;
	}
    
	cin >> a >> b;

	for(int j=0; j<i; j++)
	{
		if(s[j]==a)
			s[j]=b;
	}
	
	for(int j=0; j<i; j++)
	{
		cout << s[j] << " ";
	}

	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章