鱼C论坛

 找回密码
 立即注册
查看: 4437|回复: 7

[已解决]文章检索

[复制链接]
发表于 2022-11-17 23:32:35 | 显示全部楼层 |阅读模式
3鱼币
如何不用gets()函数完成.
最佳答案
2022-11-17 23:32:36
算了,我照着图片抄上一遍吧
但是,你直接复制粘贴,我直接复制粘贴不是更好?

  1. sh-5.1$ ls
  2. main.c
  3. sh-5.1$ cat main.c
  4. #include <stdio.h>

  5. int main(void) {
  6.     size_t count[256] = {0};
  7.     int ch;
  8.     while((ch = getchar()) != EOF) ++count[ch];
  9.     size_t result[4] = {0};
  10.     for(size_t i = 'A'; i <= 'Z'; ++i) result[0] += count[i];
  11.     for(size_t i = 'a'; i <= 'z'; ++i) result[1] += count[i];
  12.     for(size_t i = '0'; i <= '9'; ++i) result[2] += count[i];
  13.     for(size_t i = 0; i <= 255; ++i) result[3] += count[i];
  14.     result[3] -= result[0] + result[1] + result[2] + count[' '];
  15.     printf("%zu %zu %zu %zu %zu\n", result[0], result[1], result[2], count[' '], result[3]);
  16.     return 0;
  17. }
  18. sh-5.1$ gcc -g -Wall -o main main.c
  19. sh-5.1$ ls
  20. main  main.c
  21. sh-5.1$ ./main
  22. Nikon at the frontiers of science.
  23. Flash(Adobe Flash Media Rights Management Server)
  24. 21.03,-0.87,-3.97%
  25. 8 62 10 10 14
  26. sh-5.1$
复制代码
屏幕截图 2022-11-17 233040.png

最佳答案

查看完整内容

算了,我照着图片抄上一遍吧 但是,你直接复制粘贴,我直接复制粘贴不是更好?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-17 23:32:36 | 显示全部楼层    本楼为最佳答案   
算了,我照着图片抄上一遍吧
但是,你直接复制粘贴,我直接复制粘贴不是更好?

  1. sh-5.1$ ls
  2. main.c
  3. sh-5.1$ cat main.c
  4. #include <stdio.h>

  5. int main(void) {
  6.     size_t count[256] = {0};
  7.     int ch;
  8.     while((ch = getchar()) != EOF) ++count[ch];
  9.     size_t result[4] = {0};
  10.     for(size_t i = 'A'; i <= 'Z'; ++i) result[0] += count[i];
  11.     for(size_t i = 'a'; i <= 'z'; ++i) result[1] += count[i];
  12.     for(size_t i = '0'; i <= '9'; ++i) result[2] += count[i];
  13.     for(size_t i = 0; i <= 255; ++i) result[3] += count[i];
  14.     result[3] -= result[0] + result[1] + result[2] + count[' '];
  15.     printf("%zu %zu %zu %zu %zu\n", result[0], result[1], result[2], count[' '], result[3]);
  16.     return 0;
  17. }
  18. sh-5.1$ gcc -g -Wall -o main main.c
  19. sh-5.1$ ls
  20. main  main.c
  21. sh-5.1$ ./main
  22. Nikon at the frontiers of science.
  23. Flash(Adobe Flash Media Rights Management Server)
  24. 21.03,-0.87,-3.97%
  25. 8 62 10 10 14
  26. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-17 23:35:59 | 显示全部楼层
发一下样例输入
要可以复制粘贴的那种
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-11-17 23:41:59 | 显示全部楼层
人造人 发表于 2022-11-17 23:35
发一下样例输入
要可以复制粘贴的那种

$ gcc -std=c99 test23.1.c && ./a.out
test23.1.c: 在函数‘main’中:
test23.1.c:8:5: 警告:不建议使用‘gets’(声明于 /usr/include/stdio.h:638) [-Wdeprecated-declarations]
     gets(str);
     ^
/tmp/cc1Is4My.o:在函数‘main’中:
test23.1.c:(.text+0x58): 警告:the `gets' function is dangerous and should not be used.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-17 23:50:48 | 显示全部楼层
hamletroy 发表于 2022-11-17 23:41
$ gcc -std=c99 test23.1.c && ./a.out
test23.1.c: 在函数‘main’中:
test23.1.c:8:5: 警告:不建议 ...

?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-17 23:52:35 | 显示全部楼层
hamletroy 发表于 2022-11-17 23:41
$ gcc -std=c99 test23.1.c && ./a.out
test23.1.c: 在函数‘main’中:
test23.1.c:8:5: 警告:不建议 ...

把  “Sample Input” 这个后面的框框里面的内容,复制粘贴上来
我需要复制粘贴那个框框里面的内容,用来测试程序
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-17 23:59:46 | 显示全部楼层
也就是说不算3个换行符

  1. sh-5.1$ ls
  2. main.c
  3. sh-5.1$ cat main.c
  4. #include <stdio.h>

  5. int main(void) {
  6.     size_t count[256] = {0};
  7.     int ch;
  8.     while((ch = getchar()) != EOF) ++count[ch];
  9.     size_t result[4] = {0};
  10.     for(size_t i = 'A'; i <= 'Z'; ++i) result[0] += count[i];
  11.     for(size_t i = 'a'; i <= 'z'; ++i) result[1] += count[i];
  12.     for(size_t i = '0'; i <= '9'; ++i) result[2] += count[i];
  13.     for(size_t i = 0; i <= 255; ++i) result[3] += count[i];
  14.     result[3] -= result[0] + result[1] + result[2] + count[' '] + 3;
  15.     printf("%zu %zu %zu %zu %zu\n", result[0], result[1], result[2], count[' '], result[3]);
  16.     return 0;
  17. }
  18. sh-5.1$ gcc -g -Wall -o main main.c
  19. sh-5.1$ ./main
  20. Nikon at the frontiers of science.
  21. Flash(Adobe Flash Media Rights Management Server)
  22. 21.03,-0.87,-3.97%
  23. 8 62 10 10 11
  24. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-11-18 08:10:24 | 显示全部楼层
人造人 发表于 2022-11-17 23:57
算了,我照着图片抄上一遍吧
但是,你直接复制粘贴,我直接复制粘贴不是更好?

不好意思,我理解错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 03:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表