Last active
November 13, 2018 16:33
-
-
Save goodjack/0318f792022e8e54ee135c80a8d4a172 to your computer and use it in GitHub Desktop.
[CS50 2018] [Lecture 2] String (C++ version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) { | |
string s; | |
cout << "Input: "; | |
cin >> s; | |
cout << "Output: "; | |
for (int i = 0; i < s.length(); i++) | |
{ | |
cout << s[i] << "\n"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) { | |
string s; | |
cout << "Input: "; | |
cin >> s; | |
cout << "Output: "; | |
for (int i = 0, n = s.length(); i < n; i++) | |
{ | |
cout << s[i] << "\n"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) { | |
// 提示輸入使用者的名字 | |
string s; | |
cout << "Name: "; | |
cin >> s; | |
// 計算字元數直到 '\0' (aka NUL) | |
int n = 0; | |
while (s[n] != '\0') { | |
n++; | |
} | |
cout << n << "\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) { | |
string s; | |
cout << "String: "; | |
cin >> s; | |
for (int i = 0; i < s.length(); i++) | |
{ | |
int c = (int) s[i]; | |
cout << s[i] << " " << c << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment