如何在 C 中将空格和制表符替换为空?

发布于 2024-08-11 07:52:46 字数 321 浏览 4 评论 0原文

我写了这个函数:

void r_tabs_spaces(char *input) {
       int  i;
       for (i = 0; i < strlen(input); i++)
       {
        if (input[i] == ' ' || input[i] == '\t')
                    input[i] = '';
       }
}

但是,当我编译并运行它时,编译器在我尝试输入[i] = '';的行抱怨“错误:空字符常量”;

那么我怎样才能在C中做到这一点呢?

I wrote this function:

void r_tabs_spaces(char *input) {
       int  i;
       for (i = 0; i < strlen(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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果没有 2024-08-18 07:52:46

在 C 语言中,字符串是字节数组。您不能分配“空字节”,但必须将其余字节向前移动。

以下是实现此目的的一种方法:

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

请记住,C 中的文字字符串通常位于写保护内存中,因此您必须先将其复制到堆中,然后才能更改它们。例如,这通常会出现段错误:

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

您可以使用 strdup (以及其他)将字符串复制到堆:

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

编辑:根据您的评论,您可以通过记住您已经看到的事实来仅跳过初始空白非空白字符。例如:

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++));

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++));
走过海棠暮 2024-08-18 07:52:46

如果你有一个指向字符串的指针,

"            string with leading spaces"
 ^ pointer

只需移动它......

"            string with leading spaces"
             ^ pointer

例如:

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

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 */
桃气十足 2024-08-18 07:52:46

删除字符串中的一个字符的方法是将字符串的其余部分向后移动一个字符。

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

月下客 2024-08-18 07:52:46

用于

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

将指针 foo 移动到第一个不是空格或制表符的字符。

要实际从动态分配的字符串中删除字符,请使用

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文