C 練習(xí)實例98

C 語言經(jīng)典100例 C 語言經(jīng)典100例

題目:從鍵盤輸入一個字符串,將小寫字母全部轉(zhuǎn)換成大寫字母,然后輸出到一個磁盤文件"test"中保存。 輸入的字符串以!結(jié)束。

程序分析:無。

程序源代碼:

//  Created by www.o2fo.com on 15/11/9.
//  Copyright © 2015年 W3Cschool教程. All rights reserved.
//

#include<stdio.h>
#include<stdlib.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;
}

以上實例運行輸出結(jié)果為:

輸入一個字符串:
www.o2fo.com

C 語言經(jīng)典100例 C 語言經(jīng)典100例