0

我创建了一个从字符串中删除多余空格的程序。

void removeDuplicateSpaces(char **c){  //a-b---c
    char *a=*c;
    char *b=malloc(sizeof(char)*strlen(*c));  <-- allocation
    int i=0,nf=0,space=0;
    for(;a[i]!='\0';i++){
        if(a[i] != ' '){             //a-b-
            if(space>1){
                b[nf]=a[i];
                nf++;
                space=0;
            }else{
                b[nf]=a[i];
                nf++;
            }
        }else{
            space++;
            if(space==1 && i!=0){
                b[nf]=' ';
                nf++;
            }
        }
    }
    b[i]='\0';
    *c=b;
    }

int main(void) {
    char *a="    Arista    is     hiring    from   ISM   Dhanbad";
    removeDuplicateSpaces(&a); //function prototype can't be changed.
    printf("%s",a);     // ? where to deallocate.
    return 0;
}

工作演示

它工作正常。但问题是我应该在哪里释放内存,在removeDuplicateSpaces()函数中分配。printf如果我在in之后添加 free 语句,main那么它会使我的程序崩溃(signal 6 abort)。那么正确的方法是什么?


原始问题

#include<stdio.h>
main()
{
    char *foo = "    Arista    is     hiring    from   ISM   Dhanbad";
    void removeDuplicateSpaces(foo);
    printf("%s\n", foo);
}

上面给出了代码。编写一个函数removeDuplicateSpaces以删除给定字符串中的多余空格。

例如:(为清楚起见,“-”表示空格)

Input String : (without quotes)
“—-Arista——is—-hiring—-from-ISM–Dhanbad”
Output String :
“Arista-is-hiring-from-ISM-Dhanbad”
4

3 回答 3

1

最好不要从您的 removeDuplicateSpaces 函数返回一些分配的字符串。而是修改它以对已分配的缓冲区进行操作,然后您将确切知道何时可以释放分配的内存。

像这样的东西:

char *a="    Arista    is     hiring    from   ISM   Dhanbad";
char *b = (char *)malloc(sizeof(a)); // for sure result string will be less or equal to origin
removeDuplicateSpaces(&a, b);
printf("%s",b);
free(b);

然后removeDuplicateSpaces你不需要分配任何东西。

编辑: 试试这个

void removeDuplicateSpaces(const char **c){  //a-b---c
    char *a=*c;
    int i=0,nf=0,space=0;
    for(;a[i]!='\0';i++){
        if(a[i] != ' '){             //a-b-
            if(space>1){
                a[nf]=a[i];
                nf++;
                space=0;
            }else{
                a[nf]=a[i];
                nf++;
            }
        }else{
            space++;
            if(space==1 && i!=0){
                a[nf]=' ';
                nf++;
            }
        }
    }
    a[nf]='\0';
}

int main()
{
    char *a="    Arista    is     hiring    from   ISM   Dhanbad";
    char *b = (char *)malloc(strlen(a)+1);
    strcpy(b, a);
    removeDuplicateSpaces(&b); //function prototype can't be changed.
    printf("%s",b);
    free(b);
    return 0;
}
于 2013-10-20T20:18:17.593 回答
1

这是另一种方法

char* removeDuplicateSpaces( char const * src )  // show that input string is read-only
{
  char* strnospaces = calloc( 1, strlen(src)+1 );// string is filled with \0's
  for (char *t = strnospaces, *s = src; *s; )    // copy until \0
    if (!isspace(*s)) *t++=*s++; else s++;       // copy only if not space
  return strnospaces;  
}
于 2013-10-20T20:44:46.807 回答
0

您会建议以下解决方案。将函数的返回类型设为char*将返回b. 然后更改函数的调用,如以下更新的代码所示。之后你就可以free回忆了。main()printf

#include<stdio.h>

char *removeDuplicateSpaces(const char **c){  //a-b---c
    char *a=*c;
    char *b=malloc(sizeof(a));  <-- allocation
    int i=0,nf=0,space=0;
    for(;a[i]!='\0';i++){
        if(a[i] != ' '){             //a-b-
            if(space>1){
                b[nf]=a[i];
                nf++;
                space=0;
            }else{
                b[nf]=a[i];
                nf++;
            }
        }else{
            space++;
            if(space==1 && i!=0){
                b[nf]=' ';
                nf++;
            }
        }
    }
    b[i]='\0';
    *c=b;
    return b;
    }

int main(void) {
    char *a="    Arista    is     hiring    from   ISM   Dhanbad";
    char *c;
    c=removeDuplicateSpaces(&a);
    printf("%s",a);     // ? where to deallocate.a
    free(c);
    return 0;
}
于 2013-10-20T20:15:54.357 回答