简体   繁体   English

输入字符串的长度未知

[英]Input string of unknown length

I wrote this program that replaces two spaces with an '*'. 我写了这个程序,用“ *”代替两个空格。

How do I modify the code so that it does the same thing regardless of the string size? 如何修改代码,以便无论字符串大小如何都能执行相同的操作? Is it even possible only using putchar and getchar ? 甚至可能只使用putchargetchar吗?

#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;
}

The problem statement doesn't require you to store the complete input in a buffer. 问题陈述不需要您将完整的输入存储在缓冲区中。 The decision on what character to output only depends on the last two characters of input. 输出什么字符的决定仅取决于输入的最后两个字符。 Consider the following code: 考虑以下代码:

#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);
}

no need for malloc and friends at all. 完全不需要malloc和朋友。 This is a good expample for a problem that requires you to think carefully, before writing code, in order to not waste unnecessary resources on buffers. 这是一个很好的示例,该问题需要您在编写代码之前仔细考虑,以免浪费不必要的缓冲区资源。

Code for just printing: 仅打印代码:

#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;
}

Using realloc for actually changing the string: 使用realloc实际更改字符串:

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

    int main() {
        unsigned int len_max = 128;
        char *m = malloc(len_max);
        char c;             
        int counter= 0;    
        int i;
        int current_size = 256;
                printf("please input a string\n");
                while ((c = getchar()) != '\n' && c != EOF) 
                {  

            m[counter] =(char)c;
            counter++;
            if(counter == current_size)
            {
                        current_size = i+len_max;
                m = realloc(m, current_size);
            }

                }


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

            if(m[i]== ' '&&m[i+1]==' ')
            { 
                m[i]= '*';
                putchar(m[i]); 
                i = i + 2; 
                continue; 
            }
            putchar(m[i]); 
        }

        putchar('\n'); 
        free(m);
        m = NULL;
        return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM