题目要求编写一个程序读取输入,直至读到文件结尾。当命令行参数为 -u 时把输入装换成大写, 为 -l 时转化为小写,没有命令行参数时原样打印。
我的代码如下
#include
#include
#include
#define SIZE 80
#define LIM 10
int main(int argc, char * argv[])
{
int col;
int i;
int row = 0;
char str[LIM][SIZE];
while (row < LIM && fgets(str[row], SIZE, stdin) != NULL)
{
if (!strcmp(argv[1], "-u"))
for (col = 0; str[row][col] != '\0'; col++)
str[row][col] = toupper(str[row][col]);
else if (!strcmp(argv[1], "-l"))
for (col = 0; str[row][col] != '\0'; col++)
str[row][col] = tolower(str[row][col]);
row++;
}
for (i = 0; i < row; i++)
fputs(str[i], stdout);
return 0;
}
在cmd命令行里运行时,当参数为-u -l时均正常转换。当没有参数时直接报错提示中止程序。
求教各位,多谢了!