c - 如何严格使用 fgets 函数以大写/小写字母打印字符串?

标签 c string fgets

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main()
{
    FILE *fp;
    char s[50];
    fp = fopen("line.txt" , "r");

    if( fgets (s, 50, fp)!=NULL ) 
   {
      puts(s);
   }
   fclose(fp);

  return 0;
}

我知道如何使用 printf 函数以所有大写和小写字母显示字符串。但我不知道如何使用 fgets 函数来完成。我也在从文件中读取字符串,所以这对我来说是一件棘手的事情。

最佳答案

if( fgets (s, 50, fp)!=NULL ) 
{
   for( size_t ii = 0; s[ii]; ii++ )
   {
      s[ii] = toupper((unsigned char)s[ii]);
   }
   puts(s);
}

关于c - 如何严格使用 fgets 函数以大写/小写字母打印字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36758722/

相关文章:

bash 中的字符串替换 (${s//$foo/$bar}) 不适用于反斜杠

c++ - 如何将缓冲区中的字符串添加到另一个数组并打印出来?

c - openssl-rsa c 库

java - 如何在不使用数组的情况下拆分字符串?

c - 写入文件会在我想要的内容之上写入垃圾

c++ - 在不知道 UTF-8 编码的情况下拆分空白字符上的 UTF-8 编码字符串

c - fscanf 问题 - 无法从文件中提取字符串

C:使用 fgets 读取用户输入时出现段错误

c++ - C 中的 constexpr(或等价物)

C BZ2_bzDecompress 方式比 bzip2 命令慢