| 
 | 
 
 
发表于 2020-12-19 11:08:44
|
显示全部楼层
   本楼为最佳答案    
 
 
 本帖最后由 jackz007 于 2020-12-19 11:13 编辑  
 
        如果不用 gets() 很困难,在不知道字符串具体分几段的情况下,用 scanf() 读取根本不可能。 
        用 getchar() 循环读取字符串,应该是最好的解决方案。 
- #include <stdio.h>
 
  
- int main(void)
 
 - {
 
 -         char c , s[256]                                                                 ;
 
 -         for(int i = 0 ; i < 255 && (c = getchar()) != '\n' ; s[i ++] = c , s[i] = '\0') ;
 
 -         printf("%s\n" , s)                                                              ;
 
 - }
 
  复制代码 
        编译、运行实况 
- D:\00.Excise\C>cl x.c
 
 - 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
 
 - 版权所有(C) Microsoft Corporation。保留所有权利。
 
  
- x.c
 
 - Microsoft (R) Incremental Linker Version 14.28.29334.0
 
 - Copyright (C) Microsoft Corporation.  All rights reserved.
 
  
- /out:x.exe
 
 - x.obj
 
  
- D:\00.Excise\C>x
 
 - hello , world , good morning !
 
 - hello , world , good morning !
 
  
- D:\00.Excise\C>
 
  复制代码 |   
 
 
 
 |