开发者

C++ testing for special character in cstring array

I have a C string array:

char test[6] = {'\n', 't', 'e', 's', 't', '\0'}

I want to test if the string begins with a white space character (\n, \t, \r), and if so, rearrange the array so that I shift the non-whitespace characters to the front of the array and shorten the cstring by 1 for each whitespace char that needs to be removed.

So if I start with a string that looks like:

\n, \t, t, e, s, t, \0
or
\r, t, e, s, t, \0

after the function both of the arrays will look like:

t, e, s, t, \0
t, e, s, t, \0

I have two problems. My first one is my conditional test for the special characters is not working like it should

int idx = 0;
if (test[idx] != '\n' || test[idx] != '\r' || test[idx] != '\t')
    return;

That will return even if it does begin with one of those special characters.

However, that also appears to already need major improvement.

And after that, I'm not sure how to clip the string. For instance, if the string begins with a whitespace character, I need to basically remove that character, shift the othe开发者_如何转开发r characters up, and shorten the string by one each time.

Basically, I'm struggling with the logic of how to do this.

Any and all help is much appreciated. Thanks in advance!


Why not just increment the pointer forward until a non-whitespace character is found?

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


Your test, as written, is checking if the character is not equal to any of the whitespace chars. You need to check that it's not equal to all of them. You want:

int idx = 0;
if (test[idx] != '\n' && test[idx] != '\r' && test[idx] != '\t')
    return;

Assuming, then, that idx is the index of the first non-whitespace char, or the null terminator you could shorten the string like this:

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

As others have said, there are more elegant ways to do all this with isspace() and pointers, but this should give you the basic idea.


First of all, use isspace instead, it will make the code simpler and make sure the code finds whitespace characters you didn't think of.

Second, if you have to use a char[] or have to actually remove the whitespace then you will have to do a lot more work. If all you need is a pointer to the beginning of the string, it will make life a lot easier. The characters will still be in memory, but they won't appear at the beginning of the string if you use the pointer.

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.*/

If you need this string at the beginning of the char array itself, you can use memmove after the above code to shift the string over (strcpy won't work since the ranges overlap):

/* to be placed after block of code above */
memmove(test, test_ptr, strlen(test)-(test_ptr-test)+1); /* +1 for NULL not automatically copied with memmove */
/* test[0] is now the first non-whitespace character or NULL */

Or since you are using C++, you can go the std::string route:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜