用于从C中的字符串/ char数组中删除空格的函数

Joh*_*n R 6 c arrays string

问的问题在这里是非常相似,我有一个问题.区别在于我必须将一个参数传递给一个删除空格并返回结果字符串/ char数组的函数.我得到了代码工作来删除空格但由于某种原因我留下了原始数组遗留的尾随字符.我甚至尝试过strncpy,但我遇到了很多错误.

这是我到目前为止:

#include <stdio.h>
#include <string.h>
#define STRINGMAX 1000                                                      /*Maximium input size is 1000 characters*/

char* deblank(char* input)                                                  /* deblank accepts a char[] argument and returns a char[] */
{
    char *output=input;
    for (int i = 0, j = 0; i<strlen(input); i++,j++)                        /* Evaluate each character in the input */
    {
        if (input[i]!=' ')                                                  /* If the character is not a space */
            output[j]=input[i];                                             /* Copy that character to the output char[] */
        else
            j--;                                                            /* If it is a space then do not increment the output index (j), the next non-space will be entered at the current index */
    }
    return output;                                                          /* Return output char[]. Should have no spaces*/
}
int main(void) {
    char input[STRINGMAX];
    char terminate[] = "END\n";                                             /* Sentinal value to exit program */

    printf("STRING DE-BLANKER\n");
    printf("Please enter a string up to 1000 characters.\n> ");
    fgets(input, STRINGMAX, stdin);                                         /* Read up to 1000 characters from stdin */

    while (strcmp(input, terminate) != 0)                                   /* Check for que to exit! */
    {
        input[strlen(input) - 1] = '\0';
        printf("You typed: \"%s\"\n",input);                                /* Prints the original input */
        printf("Your new string is: %s\n", deblank(input));                 /* Prints the output from deblank(input) should have no spaces... DE-BLANKED!!! */

        printf("Please enter a string up to 1000 characters.\n> ");
        fgets(input, STRINGMAX, stdin);                                     /* Read up to another 1000 characters from stdin... will continue until 'END' is entered*/
    }
}
Run Code Online (Sandbox Code Playgroud)

P.P*_*.P. 13

从中移除空格后,input您没有使用nul-terminator(\0)终止它,因为新长度小于或等于原始字符串.

只需在你的for循环结束时终止它:

char* deblank(char* input)                                         
{
    int i,j;
    char *output=input;
    for (i = 0, j = 0; i<strlen(input); i++,j++)          
    {
        if (input[i]!=' ')                           
            output[j]=input[i];                     
        else
            j--;                                     
    }
    output[j]=0;
    return output;
}
Run Code Online (Sandbox Code Playgroud)


unw*_*ind 11

你没有终止输出,因为它可能会缩小,你就会把旧尾巴留在那里.

此外,我建议处理j,在循环中总是递增,然后如果不复制当前字符则必须手动递减,在某种程度上是次优的.它不是很清楚,它正在做无意义的工作(递增j),甚至在不需要时也必须撤消.相当混乱.

它写得更容易:

char * deblank(char *str)
{
  char *out = str, *put = str;

  for(; *str != '\0'; ++str)
  {
    if(*str != ' ')
      *put++ = *str;
  }
  *put = '\0';

  return out;
}
Run Code Online (Sandbox Code Playgroud)