# 407 刪除字元
```
請撰寫一程式,讀取read.txt檔案內容,將檔案中的「*」符號全部刪除,並寫入至write.txt檔案。
輸入說明 讀取read.txt檔案內容
輸出說明 寫入至write.txt檔案
#include <stdio.h>
#include <string.h>
#define M 1024
void deleteChar(char* str,char ch);
int main(){
FILE *fin = fopen("read.txt", "r");
FILE *fout = fopen("write.txt", "w");
char buf[M];
while (fgets(buf, M, fin) ){ //從fin最多取M個字元到buf
deleteChar(buf, '*');
fputs(buf, fout); //字串寫入檔案
memset(buf,0,M);
}
fclose(fin);
fclose(fout);
}
//https://www.796t.com/article.php?id=236706
void deleteChar(char str[],char ch){
int i,j;
for(i=j=0; str[i]!='\0'; i++)
if(str[i]!=ch)
str[j++]=str[i];
str[j]='\0';
}
```
# 409
```
請撰寫一程式,讀取read.txt檔案內容,將檔案中的小寫英文字母加密,加密方法為所有小寫字母向後偏移2個字母,並將結果寫入至write.txt檔案。
輸入說明 讀取read.txt檔案內容
輸出說明 寫入至write.txt檔案
#include <stdio.h>
//#include <string.h>
#define M 10
int main(){
FILE *fin = fopen("read.txt", "r");
FILE *fout = fopen("write.txt", "w");
char buf[M];
while (fgets(buf, M, fin) ){ //從fin最多取M個字元到buf
for(int i=0; buf[i]!='\0'; i++)
buf[i]+=2;
fputs(buf, fout); //字串寫入檔案
//memset(buf,0,M);
}
fclose(fin);
fclose(fout);
}
#include <stdio.h>
//#include <string.h>
#define M 10
void offset(char* str, int d);
int main(){
FILE *fin = fopen("read.txt", "r");
FILE *fout = fopen("write.txt", "w");
char buf[M];
while (fgets(buf, M, fin) ){ //從fin最多取M個字元到buf
offset(buf, 2);
fputs(buf, fout); //字串寫入檔案
//memset(buf,0,M);
}
fclose(fin);
fclose(fout);
}
void offset(char str[], int d){
for(int i=0; str[i]!='\0'; i++)
str[i]+=d;
}
```
# 501 字串轉換
```
請撰寫一程式,讓使用者輸入長度不超過10字元的數字字串,將字串轉換為整數後,輸出原本的字串內容及轉換後的結果。
提示:需將字串轉換成整數。
輸入說明 一個1~9位數的數字
輸出說明 原本的字串內容及轉換後的結果
範例輸入 123.456
範例輸出 123.456 change to 123
#include <stdio.h>
int main(){
char x[11];
scanf("%10s", x);
int sum=0;
for (int i=0; x[i] != '.'; ++i)
sum = sum*10 + x[i]-48; //字串轉成數字
printf("%s change to %d", x, sum);
}
#include <stdio.h>
#include <stdlib.h>
int main(){
char x[11];
scanf("%10s", x);
//字串轉成數字
printf("%s change to %lu", x, strtol(x, NULL, 10));
}
https://www.knowmore.cc/a/202103/80002.html
#include <stdio.h>
int main(){
float x;
scanf("%f", &x);
printf("%.3f change to %d", x, (int) x);
}
/*
char str[15];
gets(str); //str可包含空格, 以換行做結束
strlen 返回值為lu(size_t) 不含'\0'
*/
http://www2.lssh.tp.edu.tw/~hlf/class-1/lang-c/array-char.htm
【例 9】輸入五個國家的名稱按字母順序排列輸出。
```
#503 區間運算
```
請撰寫一程式,讓使用者輸入兩個正整數a、b,請輸出區間[1,a)中,a開根號為正整數且執行下方公式後的所有結果。
公式:
輸入說明 兩個正整數
輸出說明 公式計算後的結果
範例輸入 49 3
範例輸出
1
8
27
64
125
216
#include <stdio.h>
#include <math.h>
int main(){
int a, b, c=1;
scanf("%d %d", &a, &b);
for (int i=1; i<sqrt(a); ++i){
for (int j=1; j<=b; ++j)
c*=i;
printf("%d\n", c);
c=1;
}
}
```
# 505 公式計算
```
請撰寫一程式,讓使用者輸入a、b、c、d、e及f六個浮點數變數值,計算並輸出下列公式值(四捨五入至小數點後第二位)。
公式:
輸入說明 六個浮點數
輸出說明 公式計算後的結果(四捨五入至小數點後第二位)
範例輸入
-1.25 3.5
2 3
10 100
範例輸出 31.05
#include <stdio.h>
#include <math.h>
int main(){
float a,b,c,d,e,f;
scanf("%f %f %f %f %f %f", &a, &b, &c, &d, &e, &f);
if (a<0)
a*=-1;
printf("%.2f", a*floor(b) + pow(c, d)*sqrt(e) + log10(f));
}
for (int i=0; i<d; ++i)
u *=c;
float v= (int) (( a*(int)b + pow(c, d)*sqrt(e) + log10(f) + 0.005)*100)/100.0;
float a=1;
for (int i=0; i<x[3]; ++i)
a *= x[2];
float b= (int) (( x[0]*(int) x[1] + pow(x[2], x[3])*sqrt(x[4]) + log10(x[5]) + 0.005)*100)/100.0;
```
# 507 猜數字
```
請撰寫一程式,製作一個4位數的猜數字系統,讓使用者先輸入一個4位數字,為猜數字的答案,接著再輸入三組數字,若數值、位置與答案完全相同,則為A;若數值與答案相同但位置不同,則為 B,最後依序輸出猜數字的結果。
提示:每個4位數數值皆不能重複。
輸入說明 四組4位數字
輸出說明 猜數字結果
範例輸入
1350
1234
5678
1305
範例輸出
1A1B
0A1B
2A2B
#include <stdio.h>
#define N 5
int main(){
char x[N],y[3][N];
scanf("%4s", x);
for (int k=0; k<3; ++k)
scanf("%4s", y[k]);
for (int k=0; k<3; ++k){
int a=0, b=0;
for (int i=0; i<4; ++i){
if (y[k][i]==x[i])
a++;
for (int j=0; j<4; ++j)
if (j != i && y[k][j]==x[i])
b++;
}
printf("%dA%dB\n", a, b);
}
}
```
# 509 字串拆解
```
請撰寫一程式,讓使用者輸入一個用斜線(/)分隔的整數字串,字串長度不得超過128字元,將字串中的整數字元轉換為整數後輸出(以半形空格隔開),最後計算總合。
輸入說明 用斜線(/)分隔的整數字串
輸出說明 字串轉為整數的結果及總和
範例輸入 6/-3/8/12
範例輸出
6 -3 8 12
23
// 以下還沒做完
https://xiwan.io/archive/string-split-strtok-strtok-r-strsep.html
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 128
int main(){
char x[N];
scanf("%127s", x);
char *d = "-/";
char *u = strtok(x, d);
int sum=0;
while (u){
u = strtok(NULL, d);
sum+=strtol(u, NULL, 10);
printf("%s",u);
}
for (int k=0; k<strlen(x); ++k)
printf("%c", x[k]?:' ');
printf("\n%d", sum);
}
void deleteChar(char str[],char ch){
int i,j;
for(i=j=0; str[i]!='\0'; i++)
if(str[i]!=ch)
str[j++]=str[i];
str[j]='\0';
}
// 正確答案
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 128
int main(){
char x[N], z[]="1/";
scanf("%127s", x);
strcat(z,x);
int sum=0, i=0, y[N/2], j=0;
for (int i=0; i<strlen(z); ++i){
if (z[i]=='/'){
y[j] = strtol(&z[i+1], NULL, 10);
sum += y[j];
j++;
}
}
for (int k=0; k<j-1; ++k)
printf("%d ", y[k]);
printf("%d\n%d", y[j-1], sum);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 128
void deleteChar(char* str,char ch);
int main(){
char x[N], z[]="1/";
scanf("%127s", x);
strcat(z,x);
int sum=0, i=0, y;
for (int i=0; i<strlen(z); ++i){
if (z[i]=='/'){
y = strtol(&z[i+1], NULL, 10);
printf("%d ", y); //錯誤!最後數字多了一個空格
sum += y;
}
}
printf("\n%d", sum);
}
```
# 601 大小寫轉換
```
請撰寫一程式,讓使用者輸入一個字串及自然數n,n為字串的索引值,請判斷索引值為n的字元,若為大寫,則將大寫轉成小寫;若為小寫,則將小寫轉成大寫,並替換成新的字串,最後輸出大小寫轉換後的字元與字串。
輸入說明 一個字串及自然數n
輸出說明 大小寫轉換後的字元與字串
範例輸入
abcdef
3
範例輸出
The letter that was selected is: D
abcDef
#include <stdio.h>
int main(){
char x[10];
int n;
scanf("%s %d", x, &n);
x[n]>96 ? x[n]-=32 : x[n]+=32;
printf("The letter that was selected is: %c\n", x[n]);
printf("%s", x);
}
```
# 603 多重迴圈
```
請撰寫一程式,讓使用者輸入兩個正整數n、m及n*m個整數,建立n*m的二維陣列資料,請將輸入的陣列以半行逗號隔開後輸出。
輸入說明 兩個正整數n、m及n*m個整數
輸出說明 以半形逗號隔開的二維陣列資料
範例輸入
5 4
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
範例輸出
1,1,1,1
2,2,2,2
3,3,3,3
4,4,4,4
5,5,5,5
#include <stdio.h>
int main(){
int n, m;
scanf("%d %d", &n, &m);
int x[n][m];
for (int i=0; i<n; ++i){
for (int j=0; j<m; ++j)
scanf("%d", &x[i][j]);
}
for (int i=0; i<n; ++i){
for (int j=0; j<m; ++j)
printf("%d%c", x[i][j], (j<m-1) ? ',' : '\n');
}
}
```
# 605 差值計算
```
請撰寫一程式,讓使用者輸入六個不重複的整數,計算並輸出較大的三個數值和與較小的三個數值和之差。
輸入說明 六個不重複的整數
輸出說明 較大的三個數值和與較小的三個數值和之差
範例輸入1 10 20 30 40 50 60
範例輸出1 90
#include <stdio.h>
#define N 6
int main(){
int x[N];
for (int i=0; i<N; ++i)
scanf("%d", &x[i]);
int j;
for (int k=0; k<N-1; ++k){
for (int i=0; i<N-1; ++i){
if (x[i]>x[i+1]){
j = x[i];
x[i] = x[i+1];
x[i+1] = j;
}
}
}
printf("%d", (x[3]+x[4]+x[5])-(x[0]+x[1]+x[2]) );
}
```
# 607 撲克牌比大小
```
1. 請撰寫一程式,製作撲克牌比大小遊戲,讓使用者輸入兩張牌,比較兩張牌的大小並將結果輸出。
2. 撲克牌比大小規則:每張牌分別以英文及數字表示,其中S代表黑桃、H代表紅心、D代表方塊、C代表梅花,數字為1~13。首先比較花色:黑桃>紅心>方塊>梅花;當花色相同時,再比較數字:13最大、1最小。
提示:數字「0」的ASCII碼=48。
輸入說明 兩張撲克牌,以英文(S、H、D、C)及數字(1~13)表示
輸出說明 兩張撲克牌比大小結果
範例輸入1 S1 D13
範例輸出1 S1 > D13
範例輸入2 C5 C5
範例輸出2 C5 = C5
#include <stdio.h>
#include <stdlib.h>
#define N 4
char compare(char* a, char* b){
if (a[0]>b[0])
return '>';
else if (a[0]<b[0])
return '<';
else {
int u = atoi(&a[1]);
int v = atoi(&b[1]);
if (u>v)
return '>';
else if (u==v)
return '=';
else
return '<';
}
}
int main(){
char a[N], b[N];
scanf("%3s%3s", a, b);
printf("%s %c %s", a, compare(a,b), b);
}
http://bit.ly/3TMvaLC
#include <stdio.h>
#include <stdlib.h>
#define N 4
int main(){
char a[N], b[N];
scanf("%3s%3s", a, b);
if (a[0]>b[0])
printf("%s > %s", a, b);
else if (a[0]==b[0]){
int u = atoi(&a[1]);
int v = atoi(&b[1]);
if (u>v)
printf("%s > %s", a, b);
else if (u==v)
printf("%s = %s", a, b);
else
printf("%s < %s", a, b);
} else
printf("%s < %s", a, b);
}
```