C++ C++;cstring阵列的特殊特性测试

C++ C++;cstring阵列的特殊特性测试,c++,string,logic,C++,String,Logic,我有一个C字符串数组: char test[6] = {'\n', 't', 'e', 's', 't', '\0'} 我想测试字符串是否以空白字符(\n、\t、\r)开头,如果是,请重新排列数组,以便将非空白字符移到数组的前面,并将需要删除的每个空白字符的字符串缩短1 因此,如果我从一个字符串开始,如下所示: \n, \t, t, e, s, t, \0 or \r, t, e, s, t, \0 函数完成后,两个阵列将如下所示: t, e, s, t, \0 t, e, s, t, \0

我有一个C字符串数组:

char test[6] = {'\n', 't', 'e', 's', 't', '\0'}
我想测试字符串是否以空白字符(\n、\t、\r)开头,如果是,请重新排列数组,以便将非空白字符移到数组的前面,并将需要删除的每个空白字符的字符串缩短1

因此,如果我从一个字符串开始,如下所示:

\n, \t, t, e, s, t, \0
or
\r, t, e, s, t, \0
函数完成后,两个阵列将如下所示:

t, e, s, t, \0
t, e, s, t, \0
我有两个问题。我的第一个问题是我对特殊字符的条件测试没有像它应该的那样工作

int idx = 0;
if (test[idx] != '\n' || test[idx] != '\r' || test[idx] != '\t')
    return;
这将返回,即使它确实以这些特殊字符之一开头

然而,这似乎也需要重大改进

在那之后,我不知道如何剪断绳子。例如,如果字符串以空白字符开头,我需要基本上删除该字符,将其他字符上移,然后每次将字符串缩短一个

基本上,我正在为如何做到这一点的逻辑而挣扎


非常感谢您的任何帮助。提前谢谢

为什么不向前递增指针直到找到非空白字符

char* cstr = test;
while (*cstr && (*cstr == '\n' || *cstr == '\r' || *cstr == '\t'))
    ++cstr;

为什么不向前递增指针直到找到非空白字符

char* cstr = test;
while (*cstr && (*cstr == '\n' || *cstr == '\r' || *cstr == '\t'))
    ++cstr;

正如所写的,您的测试是检查字符是否不等于任何空白字符。你需要检查它是否不等于所有的。你想要:

int idx = 0;
if (test[idx] != '\n' && test[idx] != '\r' && test[idx] != '\t')
    return;
那么,假设
idx
是第一个非空白字符的索引,或者是空终止符,您可以这样缩短字符串:

int i;
for (i = 0; test[idx+i] != '\0'; i++)
    test[i] = test[idx+i];
test[i] = '\0';

正如其他人所说,使用
isspace()
和指针有更优雅的方法来完成这一切,但这应该给你一个基本的想法。

你的测试,正如所写的,是检查字符是否不等于任何空白字符。你需要检查它是否不等于所有的。你想要:

int idx = 0;
if (test[idx] != '\n' && test[idx] != '\r' && test[idx] != '\t')
    return;
那么,假设
idx
是第一个非空白字符的索引,或者是空终止符,您可以这样缩短字符串:

int i;
for (i = 0; test[idx+i] != '\0'; i++)
    test[i] = test[idx+i];
test[i] = '\0';
正如其他人所说,使用
isspace()
和指针有更优雅的方法来完成这一切,但这应该给你一个基本的想法。

首先,使用它,它将使代码更简单,并确保代码找到你没有想到的空白字符

其次,如果您必须使用
char[]
或必须实际删除空白,那么您将需要做更多的工作。如果您只需要一个指向字符串开头的指针,那么它将使您的生活更加轻松。这些字符仍将在内存中,但如果使用指针,它们将不会出现在字符串的开头

char test[] = "\ntest";
char *test_ptr = test;
while (*test_ptr && isspace(*test_ptr)) {
    ++test_ptr;
}
/*test_ptr now points to the first non-whitespace character in test, or the NULL character at the end of the string if it was all whitespace.*/
如果需要在字符数组本身的开头使用此字符串,可以在上述代码之后使用将字符串移到上方(
strcpy
将不起作用,因为范围重叠):

或者由于使用C++,可以使用<代码> STD::String 路由:

std::string test = "\ntest";
size_t start_pos = test.find_first_not_of("\r\n\t");
if (start_pos != std::string::npos) {
    test = test.substr(start_pos, std::string::npos);
} else {
    test = "";
}
//test now contains no whitespace in the beginning
首先,改用,它将使代码更简单,并确保代码找到您没有想到的空白字符

其次,如果您必须使用
char[]
或必须实际删除空白,那么您将需要做更多的工作。如果您只需要一个指向字符串开头的指针,那么它将使您的生活更加轻松。这些字符仍将在内存中,但如果使用指针,它们将不会出现在字符串的开头

char test[] = "\ntest";
char *test_ptr = test;
while (*test_ptr && isspace(*test_ptr)) {
    ++test_ptr;
}
/*test_ptr now points to the first non-whitespace character in test, or the NULL character at the end of the string if it was all whitespace.*/
如果需要在字符数组本身的开头使用此字符串,可以在上述代码之后使用将字符串移到上方(
strcpy
将不起作用,因为范围重叠):

或者由于使用C++,可以使用<代码> STD::String 路由:

std::string test = "\ntest";
size_t start_pos = test.find_first_not_of("\r\n\t");
if (start_pos != std::string::npos) {
    test = test.substr(start_pos, std::string::npos);
} else {
    test = "";
}
//test now contains no whitespace in the beginning

如果
test
实际上是一个数组,则无法更改其地址。如果
test
实际上是一个数组,则无法更改其地址。我相信这是最好的方法,但这不是编写代码的最佳方法,这是关于找出重新排列cstring数组的最佳方法背后的逻辑,就像我在问题中所展示的那样。不过,这仍然很有用,谢谢。我相信这是最好的方法,但这不是编写代码的最佳方法,而是找出重新排列cstring数组的最佳方法背后的逻辑,就像我在问题中所展示的那样。不过,这仍然很有用,谢谢