Skip to content

Instantly share code, notes, and snippets.

@FloydHsiu
Last active October 27, 2016 08:58
Show Gist options
  • Save FloydHsiu/ab0840f87b63a30a8afc09e7bed62cce to your computer and use it in GitHub Desktop.
Save FloydHsiu/ab0840f87b63a30a8afc09e7bed62cce to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
void change(char i);
int main(){
char string[5];
int j=0;
printf("1.請輸入進行編碼的五個字元:");
scanf("%c%c%c%c%c", string, string+1, string+2, string+3, string+4);
printf("輸出編碼:");
for(j=0; j<5; j++){
//呼叫change, 會把值string[j]帶著,然後跑到下面change裡面開始執行
change(string[j]);
//change執行完之後,回到這裡
}
printf("\n");
}
//void是“空”的意思,是指不需要把change計算的結果傳回去給main
void change(char i){
//呼叫之後從這裡開始執行~~~~~~~~~~
if(i>='A' && i<='Z')
{
i = (i-'A'+'a'); //char可以做運算,如果把'A'~'Z'換成int會是65~90、 'a'~'z' => 97~122
// (i - 'A') 可以算出i距離'A'有幾個字的差距,再把這個差距加到'a'上就可以大寫換小寫
printf("%c",i);
}
else if(i>='a' && i<='z')
{
i = (i - 'a' + 'A');
printf("%c",i);
}
else if(i>='0' && i<='9')
{
i='9'-i;//記住這裡的0~9都是char,不是int,而char的'0'~'9'轉int為48~57, 但其實只是要計算他們跟'9'的差距,所以直接char-char就好
printf("%d",i);
}
else printf("(Err)");
//執行完之後回到main剛剛change的位置
}
@FloydHsiu
Copy link
Author

Update#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment