C 練習實例98

C 語言經典100例 C 語言經典100例

題目:從鍵盤輸入一個字串,將小寫字母全部轉換成大寫字母,然後輸出到一個磁片檔"test"中保存。 輸入的字串以!結束。

程式分析:無。

程式源代碼:

實例

// Created by www.xuhuhu.com on 15/11/9. // // #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { FILE*fp=NULL; char str[50]; int i,len; printf("輸入一個字串:\n"); gets(str); len=strlen(str); for(i=0;i<len;i++) { if(str[i]<='z'&&str[i]>='a') str[i]-=32; } if((fp=fopen("test","w"))==NULL) { printf("error: cannot open file!\n"); exit(0); } fprintf(fp,"%s",str); fclose(fp); system("pause"); return 0; }

以上實例運行輸出結果為:

輸入一個字串:
www.xuhuhu.com

C 語言經典100例 C 語言經典100例