使用C中的指针创建无空格字符串

使用C中的指针创建无空格字符串,c,C,我试图编写一个代码,创建一个没有空格的字符串。但是我的代码不起作用 #include<stdio.h> #include<stdbool.h> bool SpaceDetect(char c) { if (c == ' '|| c == '\n' || c == '\t') return true; return false; } char *NoSpacer(char* s) { char* cs = s;

我试图编写一个代码,创建一个没有空格的字符串。但是我的代码不起作用

#include<stdio.h>
#include<stdbool.h>

bool SpaceDetect(char c)
{
     if (c == ' '|| c == '\n' || c == '\t')
         return true;
     return false;
}

char *NoSpacer(char* s)
{
     char* cs = s;
     while (*s != '\0')
     {
         if(SpaceDetect(*s))
         {
             s++;
             continue;
         }
         *cs = *s;
         cs++;
         s++;
     }
     *cs = '\0';
     return cs;
}

int main ()
{
     char test[] = " this is a test for me";
     printf("String with space is : %s\r\n", test);
     printf("Sting with no space is: %s\r\n", NoSpacer(test));
     return 0;
}
这个

使用
cs[index]
代替
cs++
并增加
索引
,并将
s
的内容存储到
cs
中,不带空格

例如

char *NoSpacer(char* s) {
        char* cs = s;
        int index = 0;
        while (*s != '\0') {
                if(SpaceDetect(*s)) {
                        s++;
                        continue;
                }
                cs[index] = *s; /* this is correct way */
                index+=1;
                //cs++; /* this is the problem */
                s++;
        }
        cs[index] = '\0';
        return cs;
}

我没有发现任何故障,也没有发现任何可能导致故障的原因。相反,我得到一个空结果

$ ./test
String with space is :  this is a test for me
Sting with no space is: 
问题是NoSpacer返回一个指向NULL的指针

 *cs = '\0';
 return cs;
NoSpacer(test)
就地修改
test
。它不应该返回任何内容。相反,请再次使用
test

#include<stdio.h>
#include<ctype.h>

void NoSpacer(char* s)
{
     char* cs = s;
     while (*s != '\0')
     {
         if(isspace(*s))
         {
             s++;
             continue;
         }
         *cs = *s;
         cs++;
         s++;
     }
     *cs = '\0';
}

int main ()
{
     char test[] = " this is a test for me";
     printf("String with space is : %s\r\n", test);
     NoSpacer(test);
     printf("Sting with no space is: %s\r\n", test);
     return 0;
}
#包括
#包括
void NoSpacer(字符*s)
{
char*cs=s;
而(*s!='\0')
{
如果(isspace(*s))
{
s++;
继续;
}
*cs=*s;
cs++;
s++;
}
*cs='\0';
}
int main()
{
char test[]=“这是对我的测试”;
printf(“带空格的字符串为:%s\r\n”,test);
NoSpacer(测试);
printf(“没有空格的Sting是:%s\r\n”,test);
返回0;
}

另请注意,我使用的是from。

发布真实代码和真实输出-建议剪切和粘贴。“带空格的字符串是…”不是
printf的可信put(“带空格的字符串是:%s\r\n”,test)
我想不出哪里需要将
'\r'
'\n'
配对?即使在绕线机上也不行…我无法重现断层。事实上,对于Achals答案的变体,甚至没有功能问题。看到chux的评论,我怀疑你远不是一个好朋友。请创建一个。在开始时将cs或s保存在某个变量中(两者都递增)。然后返回这个指针(它必须指向字符串的开头!),一切都会正常工作。然后返回tmp.OP的
*cs='\0';返回cs将返回空字符串,不会导致seg故障。OP的问题中还有其他问题。yes@chux true
*cs='\0'
返回cs不会导致seg.fault,这
printf(“没有空格的Sting是:%s\r\n”,NoSpacer(test))可能不打印任何内容。使用OP显示的代码,无法解释SEGFULT。这不太可能是OP真正关注的东西。这个答案只解决了一个功能问题,而不是OP所问的问题,即SEGFULT。但是,如果没有实际的MCVE,解决SEGFULT似乎是不可能的。不过,很好地发现了返回值错误。同意@Yunnosch谢谢您的欣赏。
 *cs = '\0';
 return cs;
#include<stdio.h>
#include<ctype.h>

void NoSpacer(char* s)
{
     char* cs = s;
     while (*s != '\0')
     {
         if(isspace(*s))
         {
             s++;
             continue;
         }
         *cs = *s;
         cs++;
         s++;
     }
     *cs = '\0';
}

int main ()
{
     char test[] = " this is a test for me";
     printf("String with space is : %s\r\n", test);
     NoSpacer(test);
     printf("Sting with no space is: %s\r\n", test);
     return 0;
}