探討數組與字串輸入的問題(C++版)_C 語言

來源:互聯網
上載者:User

對於字串問題,原來理解的不夠深刻,現在討論一些關於字串輸入的問題

   1.strlen() 返回的是數組中的字串的長度,而不是數組本身的長度。
   2.strlen()只計算可見的字元,而不把Null 字元計算在內。

那麼更有意思的在後面:

char name[16] = "abcdefg";//輸出結果是多少?cout << name << endl;name[3] = '\0';//輸出結果又是多少?cout << name << endl; 

大家猜猜 ?

# include <iostream># include <cstring># define SIZE 15using namespace std;int main(void){  char name_cin[SIZE];  char name[SIZE] = "C++owboy"; //initialized array  cout << "Hello I'm " << name;  cout << "! What is your name ? ";  cin >> name_cin;  cout << "Well " << name_cin << ", your name has ";  cout << strlen(name_cin) << " letters and is stored " << endl;  cout << "in an array of " << sizeof(name_cin) << "bytes." << endl;  cout << "your initial is " << name_cin[0] << "." << endl;  name[3] = '\0';  cout << "Here are the first 3 characters of my name : ";  cout << name << endl;  return 0;}

大家猜猜結果呢?

name字串被截斷了...

釋義: 
   數組可以用索引來訪問數組的各個字元,例如name[0]找到數組的第一個字元,name[3] = '\0';  設定為空白字元,使得這個字串在第三個字元後面即結束,即使數組中還有其他字元。

不過cin有個缺陷,就是以空白符為結束標誌,如果遇到空格和斷行符號就把這個字串輸入完了,這樣就需要用能輸入一行字串的方法來解決,但是先看看這個問題:

# include <iostream>using namespace std;int main(void){ const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name : " << endl; cin >> name; //輸入名字 cout << "Enter your favorite dessert: " << endl; cin >> dessert; //輸入甜點的名字 cout << "I have some delicious " << dessert; cout << " for you, " << name << "." << endl; return 0;}

釋義:

cin使用空白(空格、定位字元、分行符號)來定字串的邊界,cin在擷取字元數組輸入時唯讀取第一個單詞,讀取單詞後,cin將該字串放到數組中,並自動在結尾添加Null 字元'\0'
cin把Meng作為第一個字串,並放到數組中,把Liang放到輸入隊列中第二次輸入時,發現輸入隊列Liang,因為cin讀取Liang,並將它放到dessert數組中

這時如果能輸入一行資料,這個問題不就解決了嗎?
getline()、get()可以實現...

 # include <iostream>using namespace std;int main(void){  const int ArSize = 20;  char name[ArSize];  char dessert[ArSize];  cout << "Enter you name : " << endl;  cin.getline(name,ArSize);  cout << "Enter you favorite dessert : " << endl;  cin.getline(dessert,ArSize);  cout << "I have some delicious " << dessert;  cout << " for you," << name << endl;  return 0;}

釋義:

   cin是將一個單詞作為輸入,而有些時候我們需要將一行作為輸入,如 I love C++
   iostream中類提供了一些面向行的類成員函數,如getline()和get(),這兩個都是讀取一行的輸入,直到分行符號結束,區別是getline()將丟棄分行符號
   get()將分行符號保留在輸入序列中
   面向行的輸入:getline(char* cha,int num)
   getline()讀取整行,通過分行符號來確定結尾,調用可以使用 cin.getline(char* cha,int num),成員函數的方式使用,第一個參數是用來儲存輸入行的數組的名稱,第二個參數是要讀取的字元數,如果這個字元數的參數為30,則最多讀入29個字元,餘下的用於儲存自動在結尾處添加的Null 字元。
   get()儲存字串的時候,用Null 字元結尾。

如果遇到這種情況咋辦?

# include <iostream>using namespace std;int main(void){ cout << "What year was your house built? " << endl; int year; cin >> year; //char ch; //cin.get(ch); 接收分行符號 (cin >> year).get(); cout << "What is its street address ? " << endl; char address[80]; cin.getline(address, 80); cout << "Year built : " << year << endl; cout << "Address : " << address << endl; cout << "Done ! " << endl; return 0;} 

地址還沒有輸入,就結束了...
去掉上面的注意,加一個字元,接收分行符號就可以了...
註:C++程式常使用指標而不是數組來處理字串

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.