Skip to content

Instantly share code, notes, and snippets.

@housemeow
Created October 21, 2016 01:53
Show Gist options
  • Save housemeow/e6817273e51519a342ba610bead3b5e0 to your computer and use it in GitHub Desktop.
Save housemeow/e6817273e51519a342ba610bead3b5e0 to your computer and use it in GitHub Desktop.
/*
Q1.設計一個程式讓使用者可輸入一個字元,並且判斷使用者
輸入的字元是否為英文字母大小寫,如果是大寫就轉成小寫
輸出,小寫則轉成大寫輸出,不是英文字母則提醒使用者輸
入錯誤。至少可以輸入3次。
*/
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
for (int i = 0; i < 3; i++) {
cin >> ch;
if (ch >= 'a' && ch <= 'z') {
int diff = 'a' - 'A';
cout << (char)(ch - diff) << endl;
} else if (ch >= 'A' && ch <= 'Z') {
int diff = 'a' - 'A';
cout << (char)(ch + diff) << endl;
}else {
cout << "Invalid input, please input alphabet.\n";
i--;
}
}
cout << "Program is terminated.\n";
// std::string name;
// std::cout << "What is your name? ";
// getline (std::cin, name);
// std::cout << "Hello, " << name << "!\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment