Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中删除字符串中的额外空格_C_Algorithm_Whitespace - Fatal编程技术网

在C中删除字符串中的额外空格

在C中删除字符串中的额外空格,c,algorithm,whitespace,C,Algorithm,Whitespace,我有这根绳子 "go for goa" 输出应该是 "go for goa" 我想删除多余的空格。这意味着两个或多个连续的空格应替换为一个空格。我想用一个就地算法 下面是我尝试过的代码,但不起作用: #include <stdio.h> #include <stdlib.h> #include <string.h> /* Function to remove spaces in an string array */ char *removeS

我有这根绳子

"go    for    goa" 
输出应该是

"go for goa"
我想删除多余的空格。这意味着两个或多个连续的空格应替换为一个空格。我想用一个就地算法

下面是我尝试过的代码,但不起作用:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function to remove spaces in an string array */
char *removeSpaces(char *str) {
  int  ip_ind = 1;
  /* In place removal of duplicate spaces*/
  while(*(str + ip_ind)) {
    if ((*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ')) {
      *(str_ip_ind-1)= *(str + ip_ind);
    }
    ip_ind++;
  }
  /* After above step add end of string*/
  *(str + ip_ind) = '\0';
  return str;
}
/* Driver program to test removeSpaces */
int main() {
  char str[] = "go   for  go";
  printf("%s", removeSpaces(str));
  getchar();
  return 0;
}
#包括
#包括
#包括
/*函数删除字符串数组中的空格*/
char*removespace(char*str){
int ip_ind=1;
/*就地删除重复空间*/
而(*(str+ip_ind)){
如果((*(str+ip_ind)=*(str+ip_ind-1))和(*(str+ip_ind)==''){
*(str_ip_ind-1)=*(str+ip_ind);
}
ip_ind++;
}
/*在上述步骤之后添加字符串的结尾*/
*(str+ip_ind)='\0';
返回str;
}
/*用于测试RemoveSpace的驱动程序*/
int main(){
char str[]=“继续前进”;
printf(“%s”,removespace(str));
getchar();
返回0;
}

您没有检查空间。。您正在检查选项卡。将\t替换为(我指的是空格…)

问题:

  • 您正在跟踪“\t”选项卡,但必须删除 空格。
  • 此外,您正在跟踪并查找“\t”,但没有删除它的步骤(我添加了一个)。
  • 每次您不能增加
    ip_ind
    ,只有在删除未完成时,您才必须inc,因为删除完成后 场景将等于递增
  • 您的方案将因
    而失败,为避免失败,请按如下所示添加一个检查参数(方法1),或者按(方法2)从0开始从
    ip\u ind
    。(感谢@Lee Daniel Crocker)
  • 解决方案:

    你可以这样试试

    方法1:ip从一开始。

    /* Function to remove spaces in an string array */
    char *removeSpaces(char *str)
    {
      int  ip_ind = 1;
      char *ptr;
    
      if(*str)
        return str;
    
      /* In place removal of duplicate spaces*/
      while(*(str + ip_ind))
      {
        if ( (*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ') )
        {
            ptr = str + ip_ind;
            //Functionality for removal of spaces.
            do{
               *(ptr-1) = *ptr;
            }while(*ptr++ != '\0');
    
    
        }
        else //Inc only if deletion is not done.
        ip_ind++;
      }
    
     /* After above step add end of string*/
      *(str + ip_ind) = '\0';
    
     return str;
    }
    
    char *removeSpaces(char *str)
    {
      int  ip_ind = 0;
      char *ptr;
      /* In place removal of duplicate spaces*/
      while(*(str + ip_ind))
      {
        if ( (*(str + ip_ind) == *(str + ip_ind + 1)) && (*(str + ip_ind)==' ') )
        {
            ptr = str + ip_ind+1;
    
            do{
               *(ptr-1) = *ptr;
            }while(*ptr++ != '\0');
    
    
        }
        else
        ip_ind++;
      }
    
     /* After above step add end of string*/
      *(str + ip_ind) = '\0';
    
     return str;
    }
    
    方法2:ip从零开始。

    /* Function to remove spaces in an string array */
    char *removeSpaces(char *str)
    {
      int  ip_ind = 1;
      char *ptr;
    
      if(*str)
        return str;
    
      /* In place removal of duplicate spaces*/
      while(*(str + ip_ind))
      {
        if ( (*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ') )
        {
            ptr = str + ip_ind;
            //Functionality for removal of spaces.
            do{
               *(ptr-1) = *ptr;
            }while(*ptr++ != '\0');
    
    
        }
        else //Inc only if deletion is not done.
        ip_ind++;
      }
    
     /* After above step add end of string*/
      *(str + ip_ind) = '\0';
    
     return str;
    }
    
    char *removeSpaces(char *str)
    {
      int  ip_ind = 0;
      char *ptr;
      /* In place removal of duplicate spaces*/
      while(*(str + ip_ind))
      {
        if ( (*(str + ip_ind) == *(str + ip_ind + 1)) && (*(str + ip_ind)==' ') )
        {
            ptr = str + ip_ind+1;
    
            do{
               *(ptr-1) = *ptr;
            }while(*ptr++ != '\0');
    
    
        }
        else
        ip_ind++;
      }
    
     /* After above step add end of string*/
      *(str + ip_ind) = '\0';
    
     return str;
    }
    

    我甚至不知道你的函数在做什么。首先,第一次通过时,-1将在字符串开始之前访问。尝试以下方法:

    char *removeSpaces(char *str) {
        char *inp = str, *outp = str;
        int prevSpace = 0;
    
        while (*inp) {
            if (isspace(*inp)) {
                if (!prevSpace) {
                    *outp++ = ' ';
                    prevSpace = 1;
                }
            } else {
                *outp++ = *inp;
                prevSpace = 0;
            }
            ++inp;
        }
        *outp = '\0';
        return str;
    }
    

    如果条件不起作用,您的
    。我会给你看我的代码。它与您的类似,只需使用两个指针:
    back
    front

    如果
    front
    不是空格,或者
    front
    是空格而
    back
    不是空格,则需要将
    front
    复制到
    back+1

    char *removeSpaces(char *str)
    {
        if (*str == '\0') return str;
    
        char *back = str;
        char *front = str + 1;
        /* In place removal of duplicate spaces*/
        while(*front != '\0')
        {
            if (*front != ' ' || *back != ' ')    // highlight
                *(++back) = *front;
            front++;
        }
    
        /* After above step add end of string*/
        *(back + 1) = '\0';
    
        return str;
    }
    
    希望这能对您有所帮助。

    好了:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* Function to remove spaces in an string array */
    char *removeSpaces(char *str) {
      int  ip_ind = 0;
      while (*(str+ip_ind) != 0) {
        if (*(str + ip_ind++) == 32) {
          if (*(str + ip_ind) == 32) {
            int x=ip_ind;
            while (*(str + x +1) != 0) {
              *(str + x)= *(str + 1 + x++);
            }
            *(str + x)= 0;
            --ip_ind;
          }
        }
      }
      return str;
    }
    /* Driver program to test removeSpaces */
    int main() {
      char str[] = "go   for  go";
      printf("%s\n", str);
      printf("%s\n", removeSpaces(str));
      char str2[] = "go     for  go for go";
      printf("%s\n", str2);
      printf("%s\n", removeSpaces(str2));
      return 0;
    }
    

    大多数解决方案似乎不必要地复杂:

    #include <ctype.h>
    #include <stdio.h>
    
    void strip_extra_spaces(char* str) {
      int i, x;
      for(i=x=0; str[i]; ++i)
        if(!isspace(str[i]) || (i > 0 && !isspace(str[i-1])))
          str[x++] = str[i];
      str[x] = '\0';
    }
    
    int main(int argc, char* argv[]) {
      char str[] = "  If  you  gaze   into  the abyss,    the   abyss gazes also   into you.    ";
      strip_extra_spaces(str);
      printf("%s\n",str);
      return 0;
    }
    
    #包括
    #包括
    空带额外空格(字符*str){
    int i,x;
    对于(i=x=0;str[i];++i)
    if(!isspace(str[i])|(i>0&!isspace(str[i-1]))
    str[x++]=str[i];
    str[x]='\0';
    }
    int main(int argc,char*argv[]){
    char str[]=“如果你凝视深渊,深渊也会凝视你。”;
    带额外空间(str);
    printf(“%s\n”,str);
    返回0;
    }
    
    为您准备一个简单的

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
    char a[100];
    int i,t,k;
    scanf("%[^\n]s",a);
    t=strlen(a);
    for(i=0;i<t;)
    {
        if((a[i]==' ')&&(a[i+1]==' '))
        {
            for(k=i+1;k<t;k++)
            {
                a[k]=a[k+1];
            }
            t=t-1;
        }
        else
            i++;
    }
    printf("%s",a);
    return 0;
    }
    
    #包括
    #包括
    #包括
    int main()
    {
    chara[100];
    inti,t,k;
    scanf(“%[^\n]s”,a);
    t=strlen(a);
    
    for(i=0;i会告诉你最好的答案,左空格@end?这里是JS的简单翻译:

    功能条额外空间(str){
    变量i,x;
    对于(i=x=0;str[i];++i)
    如果(!isspace(str[i])|(i>0&!isspace(str[i-1]))
    str[x++]=str[i];
    str[x]='\0';
    返回str;
    }
    str=“如果你凝视深渊,深渊也会凝视你。”;
    console.log(
    带额外空格(str.split(“”)
    .join(“”)
    .替换(/\0.+/g,“”)
    .替换(/\s/g,'☐')
    )
    函数isspace(c){
    返回c='';
    
    }
    不会,因为他将ip_ind初始化为1。如果不使用额外的指针ptr,就不可能解决这个问题吗?@krrishna不,这不是因为,你指向你需要的当前位置
    ip_ind
    ,要遍历到最后,你至少需要一个变量。在这种情况下,它是
    ptr
    @giorginiava,我修复了它t解决方案。旧的,但仍然很好!谢谢。