C Exercise 98
100 Classic C Language Examples
**Problem:** Input a string from the keyboard, convert all lowercase letters to uppercase, and then output it to a disk file "test" for saving. The input string ends with !.
**Analysis:** None.
Program source code:
Example
// Created by www.runoob.com on 15/11/9.
// Copyright (c) 2015 Runoob Tutorial. All rights reserved.
//
#include
<
stdio.h
#include
<
stdlib.h
#include
<
string.h
int
main
(
)
{
FILE
fp
=
NULL;
char
str
[
50
]
;
int
i
,
len
;
printf
(
"
Enter 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 above example produces the following output:
Enter a string:
www.runoob.com