开发者

How to replace whitespaces and tabs with nothing in C?

I wrote this function:

void r_tabs_spaces(char *input) {
       int  i;
       for (i = 0; i < s开发者_如何学运维trlen(input); i++)
       {
        if (input[i] == ' ' || input[i] == '\t')
                    input[i] = '';
       }
}

However when I compile this and run it, the compiler complains that "error: empty character constant" at line where I try to input[i] = '';

How can I do this in C then?


In C, a string is an array of bytes. You can't assign an "empty byte", but you have to shift the remainder of the bytes forward.

Here's one way of how to do that:

char *write = str, *read = str;
do {
   // Skip space and tab
   if (*read != ' ' && *read != '\t')
       *(write++) = *read;
} while (*(read++));

Remember that literal strings in C are usually in write-protected memory, so you have to copy to the heap before you can change them. For example, this usually segfaults:

char *str = "hello world!"; // Literal string
str[0] = 'H'; // Segfault

You can copy a string to the heap with strdup (among others):

char *str = strdup("hello world!"); // Copy string to heap
str[0] = 'H'; // Works

EDIT: Per your comment, you can skip only initial whitespace by remembering the fact that you've seen a non-whitespace character. For example:

char *write = str, *read = str;
do {
   // Skip space and tab if we haven't copied anything yet
   if (write != str || (*read != ' ' && *read != '\t')) {       
       *(write++) = *read;
   }
} while (*(read++));


If you have a pointer to the string

"            string with leading spaces"
 ^ pointer

just move it ...

"            string with leading spaces"
             ^ pointer

for example:

#include <ctype.h>
/* ... */
char mystring[] = "            string with leading spaces";
char *pointer = mystring;
while (*pointer && isspace((unsigned char)*pointer)) ++pointer;
/* pointer now points to a (possibly empty) string with no leading spaces */


The way to remove a character of a string is to move the rest of the string one character back.


Use

foo += strspn(foo, " \t");

to move the pointer foo to the first character which is not a space or tab.

To actually remove the characters from a dynamically allocated string, use

size_t offset = strspn(foo, " \t");
size_t size = strlen(foo + offset) + 1;
foo = realloc(memmove(foo, foo + offset, size), size);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜