跳到主要內容

計算導論與 C 語言基礎:第十一週(C語言的基本成分 —— C程序中的字符串)

C程序中的字符串


第一課
1-1 字符數組字符串
給數組賦值的基本與法:
#include <iostream>
using namespace std;
int main()
{
   char a[10] = {'a','b','c','d','e','f','g','h','i','j'};
   for ( int i=0; i<10; i++)
   cout << a[i];
   return 0;
}
*若是沒有給予足夠的初始值,暫存器會以 \0 來做存取,而再匯出時則會停於 \0 之處


若是沒有給予數組常量表達式,則會自動依序補上數組:



// Example program
#include <iostream>
using namespace std;
int main()
{
   char a[ ] = {'C','h','i','n','a'};
   for ( int i=0; i<10; i++)
   cout << a[i];
   return 0;
}


*如果以一組「雙引號」所引起來的字符,即為字符串。
*再論,所有以 \0 為結尾的字符數組皆為字符串
*而在字符串中必定會以 \0 做結尾,故若是字符串的長度短於初始字符串,則視為 error


如何在兩個字符串間進行賦值
測試時間:如何將以賦值的str1 = “C++ language” 轉移到 str2 上面
tips : 要完成這件事情,記得字符串的特色 \0


#include <iostream>
using namespace std;
int main()
{
   char str1[] = "C++ language", str2[20];
   int i =0;
   while ( str1[i] !='\0'){
   str2[i] = str1[i];
   i++;
   }
   str2[i]='\0';
   cout << str1 << endl;//字符數組可以直接輸出而無需括號
   cout << str2 << endl;
   return 0;
}


如何利用二維數組輸入星期
*在撰寫語法的時候一定要清楚loop是如何形成的?要如何去構成你所要的一個循環。
// Example program
#include <iostream>
using namespace std;
int main()
{
   char weekday[7][11] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
   for (int i=0; i<7;i++){
       for ( int j=0; j<11; j++){
           cout << weekday[i][j];
       }
       cout << endl;
   }
   return 0;
}




1-2 輸入緩衝區
1-2-1 「輸入」的過程


// Example program
#include <iostream>
using namespace  std;

int main()
{
   float grade;
   cout << "enter grade:";
   while (cin >> grade)
   {
       if (grade>=85)
       cout << grade << "good" << endl;
       if (grade < 60)
       cout << grade << "fail!" << endl;
       cout << " enter grade: ";
   }
   return 0;
}


1-3 一個字符的輸入
一串字符的輸出
*在理解了程式運行中緩衝區的角色之後,如何輸入字符則一目了然。
示例一:簡單輸入字符串的方式:
int main()
{
   char c;
   cout << "enter a sentences: " << endl;
   while (cin >> c)
   cout << c;
   return 0;
}
*經由程序運行可以知道,當我們cin 一組字符的時候,程序並非在按下鍵盤的一瞬間即輸出我們的指令,而是先將輸入的字符放入緩衝區,而後藉由 Enter 鍵來讓程序搜索暫存區的資訊,而後讀出。
經由上述程序,發現一個現象:cout 程序會跳過輸入時的「空格鍵」與「Enter 鍵」
*如果程序自動忽略空格鍵與 Enter 鍵 ,該如何讓程序終止?
在輸入的最末位打入^Z,程序即終止。


*cin.get()、cin.get(char)、get.char():可將空格鍵語Enter鍵當作字符來讀取。


int main()
{
   char c;
   cout << "enter a sentences: " << endl;
   while ((c = cin.get()) != EOF) //EOF = End Of File
   cout << c;
   return 0;
}


{
   char c;
   cout << "enter a sentences: " << endl;
   while (cin.get(c) )
   cout << c;
   return 0;
}


第二課
2-1 一串字符的輸入


{
   char a[10] = "Computer";
   cout << a;//注意此處因為是輸出一個字符數組,因此才可以用a來代替a[10]
   return 0;
}
*上述示例因為以雙引號括弧,因此結尾必定默認為 \0 ,也因此可以成功地讓字符輸出。
再看這個例子
*在這個例題中,則程式仍然會以字符串的方式輸出,然而因為結尾未有 \0 ,因而使輸出產生亂碼。


*定義一個二維數組即等於定義了多個一維數組一般
{
   char weekday[7][11] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
   for ( int i =0; i<7; i++)
   cout << weekday[i] << endl;
   return 0;
}


*經過了以上的講解,可以知道 char 數組可以不用將數組的名稱完整輸入,即可得到所對應的字符結果,然而輸出的若是 int 型的結果,則不會為此結果


此時 a 所代表的是儲存這數組的起始地址。


一串字符的輸入
相較於輸出,輸入比較困難一點
對於 cin 程序而言,會自動把空格鍵與 Enter 鍵當作不同字符串的分隔符號


輸入方法二:


cin.get() , cin.getline()兩者的相同與相異:
cin.getline 遇到終止標誌符時結束,暫存區的指針會移動到終止標誌符之後(這比較常用);
cin.get 則是遇到終止字符就停止,指針不再移動(這比較符合我們一般的思維模式)。
int main()
{
   char ch[20];
   cout << "enter sentences : "<< endl;
   cin.get(ch, 10, 'o'); //一共 10-1 個字符,若還沒有到達9個字符,需看到 0 即停止輸出
   cout << ch << endl;
   return 0;
}




2-2 字符串運用例題
字符串加密


沒上完


留言

這個網誌中的熱門文章

【Quora 翻譯&整理】對於程式初學者嘗試解決問題的好網站推薦

原文出處: What are some suggested websites to solve programming problems for beginners? (2016/5/15) 此篇文章強調的是去參加project的那種網站推薦

第一章 電腦、網際網路與全球資訊網簡介

C++  How to Program 7/E 筆記 第一章 電腦、網際網路與全球資訊網簡介 學習目標 基本軟硬體觀念 物件技術觀念(類別、物件、屬性、行為、封裝、繼承) 程式語言的種類 典型的C++開發環境 業界標準物件導向素模語言——UML 沿革 網際網路、球球資訊網與Web 2.0現象的沿革 在Linux 的GNU C++ 與在Microsoft Visual C++環境中測試C++ 應用程式 *結構化程式設計(structured programming) *物件導向程式設計

【 Intro to Java Programming 】Lesson 1 : Introduction-5 (Algorithms)

This Notes is according to the udacity free course : Intro to Java Programming Lesson 1 : Introduction-5(Algorithms) Key Word : pseudocode Algorithms just like a doing things protocol+condition. Without protocol+condition, you can not make choice or do the decision. So if you want to buy car, you know the car speed and you don't consider the car price or the car condition, so it is your thought, it is the algorithm you buy a car. For computer, we give them the program ===>for them to do algorithm, but now we try to write done in Pseudocode. Pseudocode write in "spoken language" which help us to understand the problem so we can translate them into computer language ==>JAVA Code.org maybe it could help you have fun from learning code