C Exercise Example 98

100 Classic C Examples

Title:Input a string from the keyboard, convert all lowercase letters to uppercase letters, then output and save it to a disk file "test". The input string ends with !.

Program analysis:None.

Program source code:

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. All rights reserved. // #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { FILE*fp=NULL; char str[50]; int i,len; printf("Input a string:\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; }

The output of the above example is:

输入一个字符串:
www.example.com

100 Classic C Examples

other extensions