简体   繁体   English

C ++:摆脱char数组中的char

[英]C++: Getting rid of char in char array

I am creating little function that will see if char in char array is a space. 我正在创建一个小函数,它将查看char数组中的char是否为空格。 If it is, it will delete the space. 如果是这样,它将删除该空间。 So far I have: 到目前为止,我有:

void clean(char* n, int size){
for (int i = 0; i<size; i++){
        if (n[i]==' '){
            n[i]= '';
        }
}

}; };

However, I get an error: 但是,我得到一个错误:

warning: empty character constant [-Winvalid-pp-token]

My question is: how can I, withouth any libraries, get rid of the space in an char array. 我的问题是:如何在没有任何库的情况下摆脱char数组中的空间。 What should I put here: 我应该在这里放什么:

n[i]= ____ 

Thanks! 谢谢!

When you find a space you need to shuffle the rest of the string to the left. 找到空格后,您需要将字符串的其余部分移至左侧。

So the code you need is (assuming null terminated strings) 因此,您需要的代码是(假设终止字符串为空)

void clean(char* n) {
   for (int from = 0, to = 0; n[from]; ++from) {
     if (n[from] != ' ') {
        n[to] = n[from];
        ++to;
     }
   }
   n[to] = 0;
}

This will copy the string to itself, removing spaces along the way 这会将字符串复制到其自身,一路删除空格

Don't confuse string constants and character constants: 不要混淆字符串常量和字符常量:

"h"

is a string constant containing one character, plus a NULL character to mark termination. 是一个字符串常量,包含一个字符,再加上一个NULL字符以表示终止。

'h'

is a character constant, it is one character, no more no less. 是一个字符常量,它是一个字符,不多也不少。

In C++ "" is indeed an empty string, but '' is invalid syntax since a character necessarily has a value. 在C ++中, ""的确是一个空字符串,但是''是无效的语法,因为字符必须具有值。

Removing a single character from a string is more involved than this. 从字符串中删除单个字符比这要复杂得多。

If you have for example a string like this: 例如,如果有这样的字符串:

"foo bar"

Removing the space character actually consists in shifting all subsequent characters to the left. 删除空格字符实际上包括将所有后续字符向左移动。

"foo bar"
    ^
    |
    +- bar\0

And do not forget to also move the final NULL character ('\\0') so that the string ends correctly after 'r'. 并且不要忘记也移动最后一个NULL字符('\\ 0'),以便字符串在'r'之后正确结束。

If you recall that C++ standard algorithms work perfectly well with arrays, then the most elegant solution to this problem is std::remove . 如果您还记得C ++标准算法可以很好地与数组配合使用,那么解决此问题的最佳方法是std::remove Here is an example: 这是一个例子:

#include <algorithm>
#include <iostream>
#include <string.h>

void clean(char* n, int size) {
    std::remove(n, n + size, ' ');
}

int main() {
    char const* test = "foo bar";
    // just some quick and dirty modifiable test data:
    char* copy = new char[strlen(test) + 1];
    strcpy(copy, test);

    clean(copy, strlen(copy) + 1);

    std::cout << copy << "\n";

    delete[] copy;
}

Note that the array does not actually shrink in size. 请注意,数组实际上并没有缩小。 If you need actual shrinking, then you need to allocate memory for a new array, copy the requirements elements to it and release memory for the old one. 如果需要实际缩小,则需要为新阵列分配内存,将需求元素复制到其中,然后为旧阵列释放内存。


Of course, in real code you should not use dynamic arrays in the first place but use std::string : 当然,在实际代码中,您不应首先使用动态数组,而应使用std::string

#include <algorithm>
#include <iostream>
#include <string>

void clean(std::string& n) {
    n.erase(std::find(n.begin(), n.end(), ' '));
}

int main() {
    std::string test = "foo bar";
    clean(test);
    std::cout << test << "\n";
}

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

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