2013年10月20日 星期日

TQC+ C 字元字串與檔案處理 601 ~ 610

TQC+ C 試題總覽:LINK
Github 備份:LINK

/* TQC+ C - 601 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* 因為要使用到字串的涵式庫,需要include <string.h> */
int main ()
{
char str[]="Apple iPhone 4";
char pstr2[20]="Apple";
char pstr1[]=" iPod";
char str3[20];
printf("str字串的長度是%d\n", strlen(str));
/* 請注意函數名稱要正確,strlen()用來取得字串長度 */
printf("pstr2連結pstr1後的字串為:%s\n", strcat(pstr2, pstr1));
/* 請注意函數名稱要正確,strcat()將後面的字串串在前面的字串後 */
printf("str3字串如下:%s\n", strcpy(str3, pstr1));
/* 請注意函數名稱要正確,strcpy(str3, pstr1),將後面的字串複製到前面的字串 */
system("PAUSE");
return 0;
}
view raw TQCplus_C_601 hosted with ❤ by GitHub
/* TQC+ C - 602 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char str2[]="Apple iPod";
char str4[]="Apple iPad";
int n;
n=strcmp(str2, str4);
/* strcmp()比較字串的函式,如果前面大於後面則是true=1,相等為0,小於則是-1 */
if (n > 0) {
printf("%s大於%s\n", str2, str4);
} else if (n == 0) { /* 條件式相等用== */
printf("%s等於%s\n", str2, str4);
} else {
printf("%s小於%s\n", str2, str4);
}
n=strncmp(str2, str4, 5);
if (n>0) {
printf("%s前五個字元大於%s前五個字元\n", str2, str4);
} else if (n==0) {//條件式相等用==
printf("%s前五個字元等於%s前五個字元\n", str2, str4);
} else {
printf("%s前五個字元小於%s前五個字元\n", str2, str4);
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_602 hosted with ❤ by GitHub
/* TQC+ C - 603 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char str2[]="Apple iPod";
char str4[]="Apple iPad";
int n;
n=strcmp(str2, str4);
/* 比較字串的函式strcmp()有區分大小寫 */
if (n>0) {
printf("%s大於%s\n", str2, str4);
} else if (n==0) {
printf("%s等於%s\n", str2, str4);
} else {
printf("%s小於%s\n", str2, str4);
}
n=strncmp(str2, str4,5);
/* 比較前幾字的字串函式strncmp()有區分大小寫 */
if (n > 0) {
printf("%s前五個字元大於%s前五個字元\n", str2, str4);
} else if (n==0) {
printf("%s前五個字元等於%s前五個字元\n", str2, str4);
} else {
printf("%s前五個字元小於%s前五個字元\n", str2, str4);
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_603 hosted with ❤ by GitHub
/* TQC+ C - 604 */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> /* 這邊有用到getche()的功能,所以必須include <conio.h> */
#include <ctype.h> /* 英文字元間的轉換需要include<ctype.h> */
int main ()
{
char ch, ch2;
printf("請輸入一小寫的英文字母: ");
ch=getche(); /* getche()這個函式只要一偵測到有字元輸入,就會立即毒入 */
/* 將下列的字元轉為大寫 */
ch2=toupper(ch); /* 英文小寫轉成大寫的函式toupper() */
printf("\n%c的大寫是%c", ch, ch2);
/* 將下列的字元轉為小寫 */
printf("\n請輸入一大寫的英文字母: ");
scanf("%c", &ch);
ch2=tolower(ch);
printf("%c的小寫是%c\n", ch, ch2);
system("PAUSE");
return 0;
}
view raw TQCplus_C_604 hosted with ❤ by GitHub
/* TQC+ C - 605 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
char ch, ch2;
printf("請輸入一小寫的英文字母: ");
scanf("%c", &ch); /* 補上&的參照 */
/* 將下列的字元轉為大寫 */
ch2 = toupper(ch);
printf("%c的大寫是%c", ch, ch2);
while (getchar() != '\n') {
/* 這邊主要是要將使用者多輸入的東西從緩衝區消耗掉
* 如果沒有這一邊的話,
* 下次再一次使用getchar()、scanf()
* 則會從緩衝區讀取尚未用完的字
*/
continue;
}
/* 將下列的字元轉為小寫 */
printf("\n請輸入一大寫的英文字母: ");
scanf("%c", &ch); /* 補上&的參照 */
ch2 = tolower(ch);
printf("%c的小寫是%c", ch, ch2);
system("PAUSE");
return 0;
}
view raw TQCplus_C_605 hosted with ❤ by GitHub
/* TQC+ C - 606 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE *fptr;/* 宣告檔案的指標 */
char ch;
fptr=fopen("character.dat", "w");
/* 開啟一個檔案,當檔案不存在時,會創建一個,並且設定模式,"w"寫入的意思 */
printf("請輸入一字元: ");
scanf("%c", &ch);
/* 將一行多餘的字元丟掉 */
while (getchar() != '\n') {
continue;
}
/* 當讀取到*就停止 */
while (ch != '*') {
fprintf(fptr,"%c",ch); /* fprintf(檔案指標,印入格式,來源); */
/* 印到檔案裏面,平常的printf式印到銀幕給我們看,現在是印到檔案中 */
printf("請輸入一字元: ");
scanf("%c", &ch);
/* 將一行多餘的字元丟掉 */
while (getchar() != '\n') {
continue;
}
}
fclose(fptr); /* 關閉檔案 */
fptr=fopen("character.dat", "r");
/* 開啟一個檔案,並且設定模式,"r"式讀取的意思 */
printf("\n以下是您輸入的資料:\n");
while (fscanf(fptr,"%c", &ch) != EOF)
/* EOF : End Of File
* 這邊的判斷式意思是如果沒有讀到檔案尾巴,就持續讀取檔案
* 用法跟scanf一樣,僅差在多一個檔案的指標
*/
{
printf("%3c\n", ch);
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_606 hosted with ❤ by GitHub
/* TQC+ C - 607 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char str[20];/* 補上陣列大小 */
char *pstr;
printf("請輸入str字串: ");
scanf("%s", str);
printf("請輸入pstr字串: ");
pstr=(char *)malloc(sizeof(char)*20); /* 動態記憶體配置,然後補上大小 */
scanf("%s", pstr);
printf("\n您輸入str字串如下: %s\n", str);
printf("您輸入pstr字串如下: %s\n", pstr);
system("PAUSE");
return 0;
}
view raw TQCplus_C_607 hosted with ❤ by GitHub
/* TQC+ C - 608 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char str[]="Apple iPhone 4";
/* 請刪除初始的長度設定,不然就設定比字串長的長度 */
char sttr[]={'i', 'P', 'a', 'd','\0'};
/* 因為要輸出字串,字串的結尾是以'\0'作為結尾 */
char *pstr="Apple iPod";
printf("str字串如下: %s\n", str);
printf("sttr字串如下: %s",sttr);
printf("\npstr字串如下: %s\n", pstr);
/* 補上一個換行符號 */
system("PAUSE");
return 0;
}
view raw TQCplus_C_608 hosted with ❤ by GitHub
/* TQC+ C - 609 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char str1[40]="iPhone 4";
char str2[40]="Apple iPod";
char str3[40]="Apple ";
char str4[40];
strncat(str3, str1, 6);
strncpy(str4, str2+6, 5);
/* 讓str2陣列從第六個字後開始讀,
* 然後雖然iPod只有四個字,
* 但是因為字串結尾有\0,
* 所以要一併讀進來
*/
printf("%s\n", str3);
printf("%s\n", str4);
system("PAUSE");
return 0;
}
view raw TQCplus_C_609 hosted with ❤ by GitHub
/* TQC+ C - 610 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
FILE *fptr;
char name[20];
int score;
fptr=fopen("score.dat", "w");
printf("請輸入姓名: ");
scanf("%s", name);
printf("請輸入分數: ");
scanf("%d", &score);
while (score != -100) {
fprintf(fptr,"%s %d", name, score);
/* 做有關檔案讀取時,參數的第一個通常需要先告知檔案的指標 */
printf("請輸入姓名: ");
scanf("%s", name);
printf("請輸入分數: ");
scanf("%d", &score);
}
fclose(fptr);
fptr=fopen("score.dat", "r");
printf("\n以下是您輸入的資料:\n");
while (fscanf(fptr,"%s %d", name, &score) !=EOF)
printf("%-10s %3d\n", name, score);
system("PAUSE");
return 0;
}
view raw TQCplus_C_610 hosted with ❤ by GitHub




TQC+ C 試題總覽:LINK
Github 備份:LINK


本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~

沒有留言:

張貼留言