C 接受带空格的字符串

C 接受带空格的字符串,c,C,如何动态接受带空格的字符串?我尝试了gets(),但它不起作用,我不想从文件中获取输入。我想接受一个字符串而不指定字符串大小 #include<stdio.h> #include<stdlib.h> #include<string.h> char *accept() { char *s; s = (char *)malloc(sizeof(char)); gets(s); return s; } int main() {

如何动态接受带空格的字符串?我尝试了
gets()
,但它不起作用,我不想从文件中获取输入。我想接受一个字符串而不指定字符串大小

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

char *accept()
{
    char *s;
    s = (char *)malloc(sizeof(char));
    gets(s);
    return s;
}

int main()
{
    char *str;
    printf("\nEnter`enter code here` one string");
    str = accept();
}
#包括
#包括
#包括
char*accept()
{
char*s;
s=(char*)malloc(sizeof(char));
获取(s);
返回s;
}
int main()
{
char*str;
printf(“\n输入'在这里输入代码'一个字符串”);
str=accept();
}

首先,不要使用
get()
。改用
fgets()
<代码>获取()无法防止缓冲区溢出。它已从C标准C11中删除

这里,

您正在为一个字符分配内存,该字符仅足以让空字符终止字符串。你需要分配更多的空间来读一行

 s = malloc(256); //sizeof(char) is always 1. So you don't have to multiply it.
 if( fgets(s, 256, stdin) == NULL )  { /* handle failure */
如果缓冲区有足够的空间,fgets()还会读取换行符。因此,如果不需要,您可能需要删除它。 您可以使用strcspn()删除它。:


首先,不要使用
get()
。改用
fgets()
<代码>获取()无法防止缓冲区溢出。它已从C标准C11中删除

这里,

您正在为一个字符分配内存,该字符仅足以让空字符终止字符串。你需要分配更多的空间来读一行

 s = malloc(256); //sizeof(char) is always 1. So you don't have to multiply it.
 if( fgets(s, 256, stdin) == NULL )  { /* handle failure */
如果缓冲区有足够的空间,fgets()还会读取换行符。因此,如果不需要,您可能需要删除它。 您可以使用strcspn()删除它。:


通过定义用户输入的大致大小

#定义长度30

char *accept(){
    char *s = (char*)malloc(sizeof(char)*LENGTH); 
    scanf("%29[0-9a-zA-Z ]", s); 
   //scanf("%[^\n]%*s", s); //or this
    return s;
}

请注意,
29
是将被读取的最大字符数,因此
s
必须指向长度至少为1的缓冲区。

通过

#定义长度30

char *accept(){
    char *s = (char*)malloc(sizeof(char)*LENGTH); 
    scanf("%29[0-9a-zA-Z ]", s); 
   //scanf("%[^\n]%*s", s); //or this
    return s;
}
请注意,
29
是将被读取的最大字符数,因此
s
必须至少指向大小为
LENGTH-1
的缓冲区

我想接受字符串而不指定大小

  • 以硬代码编号作为字符串的初始大小开始
  • 逐字阅读。将字符添加到字符串中。如果行中的字符数超过当前大小,请增加字符串的大小
  • 这里有这样一个函数:

    char *accept()
    {
       // Initial size of the string.
       int size = 256;
       int c;
       int index = 0;
    
       char* s = malloc(size);
       if ( s == NULL )
       {
          return NULL;
       }
    
       while ( (c = getchar()) != EOF && c != '\n')
       {
          // We need one character for the terminating null character
          // Hence, if index is equal to (size-1), we need to resize the
          // string.
          if ( index == size - 1)
          {
             // Resize the string.
             size = size * 2;
             s = realloc(s, size);
             if ( s == NULL )
             {
                return NULL;
             }
          }
          s[index] = c;
          ++index;
       }
    
       // NUll terminate the string before returning it.
       s[index] = '\0';
       return s;
    }
    
    PS永远不要使用
    get
    。看见如果需要阅读一行文本,请使用
    fgets

    我想接受字符串而不指定大小

  • 以硬代码编号作为字符串的初始大小开始
  • 逐字阅读。将字符添加到字符串中。如果行中的字符数超过当前大小,请增加字符串的大小
  • 这里有这样一个函数:

    char *accept()
    {
       // Initial size of the string.
       int size = 256;
       int c;
       int index = 0;
    
       char* s = malloc(size);
       if ( s == NULL )
       {
          return NULL;
       }
    
       while ( (c = getchar()) != EOF && c != '\n')
       {
          // We need one character for the terminating null character
          // Hence, if index is equal to (size-1), we need to resize the
          // string.
          if ( index == size - 1)
          {
             // Resize the string.
             size = size * 2;
             s = realloc(s, size);
             if ( s == NULL )
             {
                return NULL;
             }
          }
          s[index] = c;
          ++index;
       }
    
       // NUll terminate the string before returning it.
       s[index] = '\0';
       return s;
    }
    

    PS永远不要使用
    get
    。看见如果需要读取一行文本,请使用
    fgets

    如果要在不指定大小的情况下读取字符串,请使用
    calloc
    。不要使用
    get
    ,而是使用
    fgets
    。请参阅。认真研究POSIX函数。另外,
    accept()
    不是一个好名字,因为网络代码(主要是服务器)使用系统函数。如果您想在不指定大小的情况下读取字符串,请使用
    calloc
    。不要使用
    get
    ,而是使用
    fgets
    。请参阅。认真研究POSIX函数。另外,
    accept()
    不是一个好名字,因为网络代码(主要是服务器)使用系统函数。1)
    %99[0-9a-zA-Z]“
    不“接受带空格的字符串”。2)
    (char*)
    不需要。3) 99如何处理
    长度
    ?4) 
    scanf()
    未检查返回值。5)
    sizeof(char)
    始终为1,那么它为什么会出现呢?建议
    fgets()
    @chux更正
    fgets()
    更好,但我只是给出一个替代方案。1)
    %99[0-9a-zA-Z]“
    不“接受带空格的字符串”。2)
    (char*)
    不需要。3) 99如何处理
    长度
    ?4) 
    scanf()
    未检查返回值。5)
    sizeof(char)
    始终为1,那么它为什么会出现呢?建议
    fgets()
    @chux更正
    fgets()
    更好,但我只是给出一个替代方案。我想你有一个问题。。考虑读取256个字符,然后<代码> s [ 256 ]=‘0’;<代码>-->UB@chux,非常正确。修好了。谢谢。如果大小大于,比如说,1kib,并且剩余的字节超过256字节,那么您可能会考虑使用final
    realloc()
    将字符串缩小到合适的大小。然而,这是一个非常小的空间优化,而且肯定只有在字符串很大的情况下才相关(而且我不完全相信给出的数字足够大)。我认为你有一个一个一个的问题。。考虑读取256个字符,然后<代码> s [ 256 ]=‘0’;<代码>-->UB@chux,非常正确。修好了。谢谢。如果大小大于,比如说,1kib,并且剩余的字节超过256字节,那么您可能会考虑使用final
    realloc()
    将字符串缩小到合适的大小。然而,这是一个非常小的空间优化,而且肯定只在字符串大的情况下才相关(我不完全相信给出的数字足够大)。