鱼C论坛

 找回密码
 立即注册
查看: 1174|回复: 4

[已解决]带你学c带你飞s1e19第二题

[复制链接]
发表于 2020-2-14 04:54:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目
要求 A:定义一个长度为 21 的字符数组,用于存放用户输入的文本;               
要求 B:如果用户输入的文本长度超过 20 个字符,且文本中存在空格,则截取至最接近末尾(第 20 个字符串)的空格;
要求 C:如果用户输入的文本长度超过 20 个字符,且文本中不存在空格,则打印前 20 个字符。
  1. #include <stdio.h>
  2. #include <string.h>

  3. /*统计每个单词的字符数储存在b
  4. *将单词字符数跟空格统计到a
  5. 判断如果a 《 20,则为位最接近末尾的空格,打印。
  6. * 将读取的字符储存在ls里如果下一个字符跟上一个都不为空格则符合超
  7. 过20个字符且不存在空格,直接打印即可 */

  8. int main()
  9. {
  10.         char str1[21], ch;
  11.         int a, b, i = 0;
  12.         char ls;

  13.         a = b = 0;


  14.         printf("请输入一行文本:");
  15.         while((ch = getchar()) != EOF)
  16.         {
  17.                 i = i + 1;
  18.                 if(i < 20)
  19.                 {
  20.                         strncpy(str1, ch, 20);
  21.                                 str1[21] = '\0';
  22.                 }
  23.                 if(ch != ' ')
  24.                 {
  25.                         b = b + 1;
  26.                         ls = ch;
  27.                         if(ls != ' ' && ch != ' ')
  28.                         {
  29.                                 strncpy(str1, ch, 20);
  30.                                 str1[21] = '\0';
  31.                         }
  32.                 }
  33.                 if(ch == ' ')
  34.                 {
  35.                         a = a + b + 1;
  36.                         b = 0;
  37.                         if(a < 20)
  38.                         {
  39.                                 strncpy(str1, ch, a);
  40.                                 str1[21] = '\0';
  41.                         }
  42.                  }

  43.         }

  44.         printf("你输入的文本是:%s\n", str1);

  45.         return 0;
  46. }
  47.                                                          
复制代码


这样写会出现
test1.c: In function ‘main’:
test1.c:19: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
/usr/include/string.h:131: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
test1.c:27: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
/usr/include/string.h:131: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
test1.c:36: warning: passing argument 2 of ‘strncpy’ makes pointer from integer without a cast
/usr/include/string.h:131: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
请输入一行文本:Fishc.com
Segmentation fault


请问为什么?哪里出现了问题?谢谢大佬
最佳答案
2020-2-14 06:16:44
  1. strncpy(str1, ch, 20);
复制代码


https://baike.so.com/doc/398560-421965.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 06:16:44 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +5 鱼币

  1. strncpy(str1, ch, 20);
复制代码


https://baike.so.com/doc/398560-421965.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 07:31:08 From FishC Mobile | 显示全部楼层

回帖奖励 +5 鱼币

应该使用sprintf(),自己去小甲鱼函数快查。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 08:14:32 | 显示全部楼层

回帖奖励 +5 鱼币

用安全函数,普通函数很容易造成内存问题(去C语言的函数快查)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 10:18:00 | 显示全部楼层
对,应该用安全函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-11 20:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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