CH9 CStrings
本文為 2021-Fall 學期旁聽台大資管系孔令傑教授開授的 Programming Design 所記錄的課程筆記。課程內容程式碼可以參閱我的 Github repo: C++ Programming-Design-2021-Fall
Characters
char
- Use one byte (–128 to 127) to store English letters, numbers, symbols, and special characters(但不能儲存中文符號)
- It is also an
integer
! 字元本身儲存的是一個整數,再經由ASCII code
encode成我們看到的符號,因此每個char其實都可以轉成int,也可以最整數運算,參考9_1.cpp, 9_2.cpp
The C++ standard library <cctype>
contains some useful functions for processing characters.
int islower(int c)
int isupper(int c)
int tolower(int c)
int toupper(int c)
int isalpha(int c)
int isdigit(int c)
int isalnum(int c)
int isprint(int c)
int isspace(int c)
int ispunct(int c)
C strings
String
在C++中Strings有兩種形式:
- C strings as character arrays, A character array can be initialized as a usual array.
- C++ strings as objects. (後面章節會提)
A special way to input a string
在C++中char arrays被用來作為string,因此很多opration都會為了char arrays被overloaded,exp: cin >>, cout <<
對一般array來說沒辦法一次輸入或輸出多個元素但char array可以
char str[10];
cin >> str; // if we type "abcde"
cout << str[0]; // a
cout << str[2]; // c
int values[5] = {0};
cout << values; // an address
char str[10];
cin >> str; // if we type "abcde"
cout << str; // abcde
The null character
When we use cin >> to input a string, a null character \0
will be appended at the end automatically.
\0
is an escape sequence. It marks the end of a string.- When you declare a character array of length n, you can store a string of length at most n – 1.
- A C string may be initialized with a double quotation
- char s[100] = "abc"
char a[100] = "abcde FGH";
cout << a << "\n"; // abcde FGH
char b[100] = "abcde\0FGH";
cout << b << "\n"; // abcd
One may also initialize a C string by assigning multiple characters.
- char s[100] = c;
- No null character will be appended (though uninitialized values will be initialized to 0, which is the null character).
- = is overloaded for “a C string” and “some characters” in different ways.
Comparisons
Will a null character be appended?
char s[10] = "abc"; // Yes
char s[100] = {'a', 'b', 'c'}; // No
cin >> s; // Yes
cin >> s[0]; // No
String assignments
用""來 assign string 只能用在宣告時,宣告後char array variable存的就是address了
char s[100];
s = "this is a string"; // compilation error!
How to explain the outputs of this program?
char c[100] = {0}; // (1)
cin >> c; // (2) "123456789"
cin >> c; // (3) "abcde";
cout << c << "\n"; // "abcde"
c[5] = '*';
cout << c << "\n"; // (4) "abcde*789"
// (1) 0000000……0
// (2) 123456789\0
// (3) abcde\0789\0
// (4) abcde*789\0
Array boundary
C++ does not check array boundary!
char a[5] = {0};
cin >> a; // "123456789"
cout << a; // "123456789" or an error
A strange case
並不是因為空白鍵被當作C strings的結尾,而是因為cin
char a1[100] = {0};
cin >> a1; // "this is a string"
cout << a1; // "this"
char a2[100] = {'a', 'b', ' ', 'c', '\0', 'e'};
cout << a2; // ab c
cin >> vs. cin.getline()
cin >>
splits the input stream into char pieces according to white spaces. The same thing happens for the newline character
and tab
.
To input a string with white spaces, use cin.getline()
.
char a[100] = {0};
char b[100] = {0};
cin >> a >> b; // this is
cout << a << "\n"; // this
cout << b << "\n"; // is
char a[100];
cin.getline(a, 100); // Hi, it's me
cout << a << "\n"; // Hi, it's me
String literals and character pointers
A character pointer may also be initialized as a string literal.
char* p = "12345";
cout << p + 2 << "\n"; // 345
- When we do so, the system allocates space storing “12345”.
- That space is
read-only
. - p stores the address of that space.
char a[100] = {0};
a = "123"; // compilation error
char* p;
p = "abc"; // okay
char a[100] = "12345";
char* p = a;
p = "abc"; // does not affect a
cout << p << "\n"; // abc
cout << a << "\n"; // 12345
char a[100] = "12345";
char* p = a;
p = "abc";
cout << p << "\n";
cin >> p; // run-time error 因為character pointer為read-only
Main function arguments
我們可以在main function傳入arguments
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
for(int i = 0; i < argc; i++)
cout << argv[i] << "\n";
return 0;
}