C 如何用单个空间替换多个空间?

C 如何用单个空间替换多个空间?,c,string,C,String,我正在寻找一个函数,它可以减少字符串中的多个空格字符' 例如,对于给定的字符串s: s="hello__________world____!" 函数必须返回“hello\u world!” 在python中,我们可以通过regexp简单地执行以下操作: re.sub("\s+", " ", s); 在C标准库中没有这样的函数。必须编写函数才能这样做,或者使用第三方库 下面的函数应该可以做到这一点。使用源字符串作为目标指针就地执行操作。否则,请确保目标缓冲区的大小足够大 void simpli

我正在寻找一个函数,它可以减少字符串中的多个空格字符
'

例如,对于给定的字符串
s

s="hello__________world____!"
函数必须返回“hello\u world!”

在python中,我们可以通过regexp简单地执行以下操作:

re.sub("\s+", " ", s);

在C标准库中没有这样的函数。必须编写函数才能这样做,或者使用第三方库

下面的函数应该可以做到这一点。使用源字符串作为目标指针就地执行操作。否则,请确保目标缓冲区的大小足够大

void
simplifyWhitespace(char * dst, const char * src)
{
    for (; *src; ++dst, ++src) {
        *dst = *src;
        if (isspace(*src))
            while (isspace(*(src + 1)))
                ++src;
    }

    *dst = '\0';
}

我刚刚做了这个,希望能有所帮助。

一个修改字符串的版本,如果必须保留原始字符串,请在副本上运行它:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char word[100];
    gets(word);
    //the word has more than a single space in between the words
    int i=0,l,j;
    l=strlen(word);
    for (i=0;i<l;i++)
    {
        if(word[i]==' '&&word[i+1]==' ')
        {
            for(j=i+1;j<l;j++)
            word[j]=word[j+1];
        }
    }
    puts(word);
    return 0;
}
void压缩_空间(char*str)
{
char*dst=str;
对于(;*str;++str){
*dst++=*str;
if(isspace(*str)){
do++str;
while(isspace(*str));
--str;
}
}
*dst=0;
}

我只是在学习C,所以我使用了更多的基本代码。我正在阅读“C编程语言”的第一章,我试图找到其中一个任务集的答案

这就是我想到的:

#include <stdio.h>

int main()
{
    /* Set two integers:
       c is the character being assessed,
       lastspace is 1 if the lastcharacter was a space*/
    int c, lastspace;
    lastspace = 0;

    /* This while loop will exit if the character is EOF

       The first "If block" is true if the character is not a space,
       and just prints the character
       It also tells us that the lastcharacter was not a space

       The else block will run if the character is a space

       Then the second IF block will run if the last character
       was not also a space (and will print just one space) */

    while((c = getchar()) != EOF){
        if (c != ' '){
            putchar(c);
            lastspace = 0;
        }
        else {
            if (lastspace != 1)
                    putchar(c);
            lastspace = 1;
        }
    }

    return 0;
}
#包括
int main()
{
/*设置两个整数:
c是正在评估的角色,
如果lastcharacter是空格,则lastspace为1*/
int c,lastspace;
lastspace=0;
/*如果角色为EOF,则该while循环将退出
如果字符不是空格,则第一个“If block”为真,
然后打印角色
它还告诉我们最后一个字符不是空格
如果字符是空格,则else块将运行
如果最后一个字符
也不是一个空格(将只打印一个空格)*/
而((c=getchar())!=EOF){
如果(c!=''){
普查尔(c);
lastspace=0;
}
否则{
if(lastspace!=1)
普查尔(c);
lastspace=1;
}
}
返回0;
}
希望有帮助! 另外,我很清楚,这段代码可能没有经过优化,但对于像我这样的初学者来说,应该很容易理解


谢谢,菲尔。另一种方法是只打印第一次出现的空格,直到下一个角色出现。这是我的暴力解决方案

#include<stdio.h>
typedef int bool;
#define True  1
#define False 0
int main()
{
        int t;
        bool flag = False;

        while ((t = getchar()) != EOF)
                if (t == ' ' && !flag)
                {
                        putchar(' ');
                        flag = True; // flag is true for the first occurence of space
                }

                else if(t == ' '&& flag)
                        continue;
                else
                {
                        putchar(t);
                        flag = False;
                }

        return 0;
}
#包括
typedef int bool;
#定义真1
#定义False 0
int main()
{
int t;
布尔标志=假;
而((t=getchar())!=EOF)
如果(t=''&&!标志)
{
putchar(“”);
flag=True;//第一次出现空格时,flag为True
}
else if(t=''&&flag)
继续;
其他的
{
putchar(t);
flag=False;
}
返回0;
}
希望有帮助。

#包括
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char word[100];
    gets(word);
    //the word has more than a single space in between the words
    int i=0,l,j;
    l=strlen(word);
    for (i=0;i<l;i++)
    {
        if(word[i]==' '&&word[i+1]==' ')
        {
            for(j=i+1;j<l;j++)
            word[j]=word[j+1];
        }
    }
    puts(word);
    return 0;
}
#包括 #包括 int main() { 字符字[100]; 获取(单词); //单词之间有不止一个空格 int i=0,l,j; l=strlen(单词);
对于(i=0;i)您到目前为止尝试了什么。您到目前为止做了什么来尝试解决这个问题?我想一个可以减少多空间的函数,但它不起作用,可能是针对递归问题,但是调用方需要确保dest指向比strlen(src)大的块或者这将破坏内存。为什么要大?dest必须至少与src的大小相同。@卢卡:如果它必须至少是
strlen(src)
,那么为什么代码会变成
char dest[strlen(src)+1];
?;)它成功了。函数返回字符串被修剪,但结尾有一个特殊字符。@mezgani:我解决了这个问题,我在迭代后为空字符添加了1,但不需要。我已经执行了这个函数,但如果我插入“Empty”我将收到0输出!我声明这不是它应该做的…对我来说对你的输入有效。我不明白你所说的“接收0输出”是什么意思。我声明你错了。;-)当我编译此语句并输入字符*时,它将删除所有元素,我的输入“不知何故为空”,结果是“”所以字面上是空的,如果你告诉我我做错了什么,我会很高兴:-@Idelic你怎么做
++str
。在这种情况下它和
str++
一样吗?