C 长度未知的输入字符串

C 长度未知的输入字符串,c,arrays,if-statement,input,C,Arrays,If Statement,Input,我编写了这个程序,用“*”替换两个空格 如何修改代码,使其在不考虑字符串大小的情况下执行相同的操作?甚至可以只使用putchar和getchar #include <stdio.h> int c; char buffer[256]; int counter= 0; int i; int main() { while ((c = getchar()) != '\n'&&c!=EOF) {

我编写了这个程序,用“*”替换两个空格

如何修改代码,使其在不考虑字符串大小的情况下执行相同的操作?甚至可以只使用
putchar
getchar

#include <stdio.h>

int c;             
char buffer[256];  
int counter= 0;    
int i;             

int main()
{

while ((c = getchar()) != '\n'&&c!=EOF) {  

    buffer[counter] =c;
    counter++;

    if (counter >=255) {
        break;
    }
 }

 for(i=0; i<256; i++) {

    if(buffer[i]== ' '&&buffer[i+1]==' ')
    { 

        buffer[i]= '*';

        putchar(buffer[i]); 

        i = i + 2; 

        continue; 
    }
    putchar(buffer[i]); 
}

putchar('\n'); 
return 0;
}
#包括
INTC;
字符缓冲区[256];
int计数器=0;
int i;
int main()
{
而((c=getchar())!='\n'&&c!=EOF){
缓冲器[计数器]=c;
计数器++;
如果(计数器>=255){
打破
}
}

对于(i=0;i仅用于打印的代码:

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

int main() 
{
    char prev = EOF, curr;
    while ((curr =(char)getchar()) != '\n' && curr != EOF) 
            {  

                if(curr==' '&&prev==' ')
                {
                    curr = '*';
                    prev = EOF;

                }
                if (prev != EOF)
            putchar(prev);
        prev = curr;
            }
            putchar(prev);
    return 0;
}

问题陈述不要求你把完整的输入存储在一个缓冲区中。关于什么字符输出的决定取决于输入的最后两个字符。

#include <stdio.h>

int main(void)
{
  // two variables for the last two input characters
  int c = EOF, last = EOF;
  while ((c = getchar()) != EOF)
    {
      // if both are a space, store a single '*' instead
      if (c == ' ' && last == ' ')
        {
          c = '*';
          last = EOF;
        }
      // print the output, and shift the buffer
      if (last != EOF)
        putchar(last);
      last = c;
    }
  // take care of the last character in the buffer after we see EOF
  if (last != EOF)
    putchar(last);
}
#包括
内部主(空)
{
//最后两个输入字符的两个变量
int c=EOF,last=EOF;
而((c=getchar())!=EOF)
{
//如果两者都是空格,则存储单个'*'
如果(c=''&&last='')
{
c='*';
last=EOF;
}
//打印输出,并移动缓冲区
如果(最后一个!=EOF)
普查尔(最后);
last=c;
}
//在我们看到EOF后,注意缓冲区中的最后一个字符
如果(最后一个!=EOF)
普查尔(最后);
}

根本不需要malloc和朋友。这是一个很好的例子,用于解决需要您在编写代码之前仔细考虑的问题,以避免在缓冲区上浪费不必要的资源。

小心,返回一个
int
,这对
EOF
检查非常重要。您可能也不应该将
EOF
在缓冲区中。请记住,如果输入的行小于256个字符,则
缓冲区
的其余元素将是不确定的(并且似乎是随机的)打印输入时不应使用then。因此,我编辑了代码,但我仍然不清楚如何实现代码来读取和输入未知长度的字符串。您的编辑使while循环无限。该
|
应该是
&
。否则,要中断循环
c
必须是
'\n'
EOF
同时进行,这是不可能的。哦,谢谢。@WhozCraig你能给我一个提示,告诉我如何实现代码来做同样的事情,考虑到输入字符串的大小吗?我只是不明白它应该如何工作。为什么你甚至要存储完整的输入?大小为2的环形缓冲区对你的用例来说不够吗?谢谢对于你的答案,但对于我的练习,我只允许使用putchar、getchar,while loops和ifs,我应该在前面提到。这可能吗?你的意思是你不能使用
realloc
?是的,我不能。我已经试了一整天了,但我似乎无法正确使用它。我的意思是你可以使用
realloc
?然后是一个缓冲区长度2就足够了,last=c如何移动缓冲区?我不太明白,也许有更聪明的方法,但想法是将输入的当前字符存储在
c
中,之前的字符存储在
last
last=c
只意味着在获取下一个I之前将当前字符保存为last来自getchar的nput。代码并不重要,它只是用来说明不需要大缓冲区这一点。好的,还有最后一个问题,修改我的原始代码,使其打印未知大小的输出?或者我可以完全扔掉我的原始代码吗?我相信你可能可以放弃第一个循环,修改你的second循环开始工作。:)
#include <stdio.h>

int main(void)
{
  // two variables for the last two input characters
  int c = EOF, last = EOF;
  while ((c = getchar()) != EOF)
    {
      // if both are a space, store a single '*' instead
      if (c == ' ' && last == ' ')
        {
          c = '*';
          last = EOF;
        }
      // print the output, and shift the buffer
      if (last != EOF)
        putchar(last);
      last = c;
    }
  // take care of the last character in the buffer after we see EOF
  if (last != EOF)
    putchar(last);
}