Skip to content

Instantly share code, notes, and snippets.

@goodjack
Last active November 13, 2018 16:33
Show Gist options
  • Save goodjack/0318f792022e8e54ee135c80a8d4a172 to your computer and use it in GitHub Desktop.
Save goodjack/0318f792022e8e54ee135c80a8d4a172 to your computer and use it in GitHub Desktop.
[CS50 2018] [Lecture 2] String (C++ version)
#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";
}
}
#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";
}
}
#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";
}
#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