简体   繁体   English

删除额外的空格 C++

[英]Remove additional spaces C++

I have the homework assignment where I should remove all leading spaces in the char array.我有家庭作业,我应该删除 char 数组中的所有前导空格。 If there is a space in the beginning of the array, it should be deleted too.如果数组开头有空格,也应该删除。 Also, I have to take care about an empty array.另外,我必须照顾一个空数组。 I can't use <cstring> and <string.h> .我不能使用<cstring><string.h> Also, I can't create any additional arrays.此外,我无法创建任何额外的数组。 I googled a lot, but I have no idea how to solve this problem.我用谷歌搜索了很多,但我不知道如何解决这个问题。 Here is what I have so far.这是我到目前为止所拥有的。

void clean(char* tab) 
{
    //char *p = tab;
    for (int i = 0; i <= sizeof(tab); i++) 
    {
        if ((tab[i] = ' ') && (tab[i + 1] = ' '))
        {
            tab[i] = tab[i + 1];
        }
    }
}

int main()
{
    char tab1[] = "   h qa  w e ";
    cout << tab1 << endl;
    clean(tab1);
    cout << tab1 << endl;
}

Will be very gratefull if someone can give me a hint how to figure it out.如果有人能给我一个提示如何解决它,我将不胜感激。

Regard each space as a "free space" in the array, one which you should fill.将每个空间视为数组中的“空闲空间”,您应该填充该空间。

You can maintain two pointers to different places in your char array.您可以维护两个指向 char 数组中不同位置的指针。 One to the first available space, another to the currently character you examine.一个指向第一个可用空间,另一个指向您检查的当前角色。

You only need to figure out these questions:你只需要弄清楚这些问题:

  1. How would you fill up the spaces?你会如何填满这些空间?
  2. How would you know when you're done?你怎么知道什么时候完成?

You have a few separate problems here.你在这里有几个单独的问题。

One is that you've mis-typed your comparisons: if ((tab[i] = ' ') && (tab[i+1] = ' ')) should undoubtedly be: if ((tab[i] == ' ') && (tab[i+1] == ' ')) .一是您错误地输入了比较: if ((tab[i] = ' ') && (tab[i+1] = ' '))无疑应该是: if ((tab[i] == ' ') && (tab[i+1] == ' '))

Second, you're using sizeof(a pointer) , where you really wanted strlen(a pointer) (or at least something that works similarly, scanning for a NUL character that signals the end of the string).其次,您正在使用sizeof(a pointer) ,在那里您确实需要strlen(a pointer) (或至少工作类似的东西,扫描表示字符串结束的 NUL 字符)。

Third, the algorithm your loop implements doesn't seem to reflect the intent of removing leading spaces very well (if at all).第三,您的循环实现的算法似乎并没有很好地反映去除前导空格的意图(如果有的话)。 I'd scan for the first thing that wasn't a space, then copy the remainder of the string so it starts at the beginning of the string.我会扫描第一个不是空格的东西,然后复制字符串的其余部分,使其从字符串的开头开始。

[Sorry, but I'm not going to post actual code or much more detail than that for a homework assignment.] [对不起,但我不会发布实际代码或比家庭作业更详细的代码。]

Those codes work with absolutely no dependency on nor and they don't allocate any temporary buffers.这些代码完全不依赖于也不工作,也不分配任何临时缓冲区。 Note that a home made copy function must be defined to copy string content when a space is being removed.请注意,必须定义自制copy函数以在删除空格时复制字符串内容。

Note that this code is easy to understand but may/should be optimized to minimize copies if performance is a requirement.请注意,此代码易于理解,但如果需要性能,则可能/应该优化以最小化副本。

As it's unclear what you exactly want...因为目前还不清楚你到底想要什么......

This one removes all spaces in the string.这将删除字符串中的所有空格。

#include <iostream>

void copy( char* to, char* from )
{
    int i = 0;
    while ( true ) // risky in case char* has no EOS character!
    {
        to[i] = from[i];
        if ( from[i] != '\0' )
            ++i;
        else
            break;
    }
}

void clean( char* tab ) 
{
    int i = 0;
    while ( tab[i] != '\0' )
    {
        if ( tab[i] == ' ')
        {
            copy( tab + i, tab + i + 1 );
            // do not increment i, to test tab[i] that was newly copied from tab[i+1]
        }
        else
        {
            ++i;
        }
    }

}

int main() {

    char tab1[] = "   h qa  w e ";
    std::cout << tab1 << std::endl;

    clean(tab1);

    std::cout << tab1 << std::endl;
}

https://ideone.com/Yv4aqL https://ideone.com/Yv4aqL

If you only want to remove leading spaces , it's even easier, just change the clean function:如果您只想删除前导空格,那就更简单了,只需更改clean功能:

void clean( char* tab ) 
{
    int i = 0;
    while ( tab[i] == ' ' )
    {
        copy( tab + i, tab + i + 1 );
    }
}

https://ideone.com/RIAsGt https://ideone.com/RIAsGt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM