Arvin Liu @ Sprout
就是一個字嘛! (character)
電腦只有0跟1啊!
zh.wikipedia.org/wiki/ASCII
char c1 = 97;
std::cout << c1 << std::endl;
a
int c1 = 'a';
std::cout << c1 << std::endl;
97
char c1 = 'a';
std::cout << c1 << std::endl;
char c1 = '
';
std::cout << c1 << std::endl;
char c1 = 10;
std::cout << c1 << std::endl;
(escape character)
(escape character)
'''
'a'
常見特殊字元 | 表示方法 |
---|---|
換行 | '\n' |
水平定位 | '\t' |
字串結尾 | '\0' |
' | '\'' |
\ | '\\' |
" | '\"' |
回車 |
'\r' |
倒退 | '\b' |
#include <iostream>
using namespace std;
int main(){
char c;
while( cin >> c ){
// 做你的事情
}
}
#include <iostream>
using namespace std;
int main(){
char c;
while( cin >> c ){
char v = c + 3;
if ( v > 'z' )
v -= 26;
cout << v;
}
cout << '\n';
}
#include <iostream>
using namespace std;
int main(){
char c;
while( cin >> c ){
char v = ((c - 'a') + 3) % 26 + 'a';
cout << v;
}
cout << '\n';
}
字串是一種數列。
--> 陣列 !
char S[] = {'a','p','p','l','e','\0'};
std::cout << S << std::endl;
apple
char S[] = {'a','p','p','l','e',0};
std::cout << S << std::endl;
char S[] = "apple";
std::cout << S << std::endl;
==
char S[] = {'a','\n','p','\t','e',0};
std::cout << S << std::endl;
char S[] = "a\np\te";
std::cout << S << std::endl;
==
char S[100];
std::cin >> S;
空白字元三巨頭 : ' ', '\t', '\n'
char S[100];
while(cin >> S)
cout << "I get: " << S << endl;
Input:
abc def
ghi
Output:
I get: abc
I get: def
I get: ghi
要#include <cstring>喔!
char S[100];
cin >> S;
cout << strlen(S);
Input:
abcdef
Output:
6
char S[100];
cin >> S;
cout << (strcmp(S,"QAQ") == 0);
Input:
abcdef
-------
QAQ
Output:
1
----
0
char A[100], B[100];
cin >> B;
strcpy(A, B);
cout << A;
Input:
abcdef
Output:
abcdef
(Optional)
char A[8] = {'a','b'};
A = {'c' , 'd'};
char B[8] = "ab";
B = "cd"; if(A == B){}
你不會寫下面這樣:
所以你也不該寫下面這樣:
char A[8]="12345678";
char B[8]="Peipei";
std::cout << A << std::endl;
如果你們用這份code,
需要-fpermissive才可以通過編譯。
char A[8]="12345678";
char B[8]="Peipei";
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | '\0' |
---|
A
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---|
A
B
P | e | i | P | e | i | '\0' |
---|
> 宣告要多一點空間。
會移動的字串 - Marquee (Optional)
ㄚ不是輸出就不能改了?
常見特殊字元 | 表示方法 |
---|---|
換行 | '\n' |
水平定位 | '\t' |
字串結尾 | '\0' |
' | '\'' |
\ | '\\' |
" | '\"' |
回車 |
'\r' |
倒退 | '\b' |
char S[] = "appleeee\rbanana";
std::cout << S << std::endl;
// bananaee
char S[] = "0123\baaaa";
std::cout << S << std::endl;
// 012aaaa
#include <windows.h>
int main(){
Sleep(3000);
// 睡3000ms。
}
windows
#include <unistd.h>
int main(){
usleep(3000);
// 睡3000ms。
}
非windows
#include <windows.h>
using namespace std;
int main(){
char A[] = "ABCD ";
char B[] = " ABCD ";
cout << A ;
Sleep(3000);
cout << '\r';
cout << B ;
}
#include <iostream>
#include <cstring>
#include <windows.h>
using namespace std;
int main(){
char S[] = "OAO - QAQ ";
int slen = strlen(S), starting = 0;
while(true){
cout << '\r';
for(int i=starting, j=0 ; j<slen ; i++,j++ ){
cout << S[(i)%slen];
}
starting ++;
Sleep(100);
}
}