C C 輸入帶空格的字串

2021-09-25 20:19:09 字數 536 閱讀 1207

機試的時候碰到的問題,當時腦子卡克到想不出來怎麼處理,用了一種賊複雜的方法,,現在整理下!

#include #include int main()

一句gets解決的事情,我居然tm沒想起來。。。。太羞恥了

#include #include int main()

#include #include int main()

#include using namespace std;

string s;

int main(){

getline (cin,s);

coutconst int n = 20;

char x[n+1];

int main(){

cin.getline (x,n+1); //這裡最好寫n+1,因為字串末尾的的'\0'也包含在這個長度之內.

cout<

C C 中帶空格的字串的輸入

對於字元陣列 方法一 getline 讀入整行資料,使用回車鍵輸入的換行符來確定輸入結尾。呼叫方法 cin.getline str,len 第乙個引數str用來儲存輸入行的陣列名稱,第二個引數是要讀取的字元數。方法二 cin.get str,len 兩者都是讀取一行輸入,直至換行符。然後,getli...

C C 中輸入帶空格的字串用法總結

在c c 中,傳統的輸入流scanf s str 和cin str,遇到空格會返回空格之前的字串。但是在很多應用中,我們需要字串中可以同時包含空格,那麼前面的兩種方法現在就不可用了,在c c 中給我們提供了那些輸入流處理方式呢。下面是我總結的幾種常用的方法 1 gets char str 需要包含標...

C 連續輸入帶空格的字串

測試檔案 測試1 include include include using namespace std int main 測試結果 顯然cin s不能夠接受空格的輸入 測試2 include include include using namespace std int main 測試結果 顯然g...