gpt4 book ai didi

c - 如何从 char 数组中清除单个字符?

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:04 24 4
gpt4 key购买 nike

我正在尝试从数组中删除单个字符,而不使用 strcpy 等标准库函数。我该怎么做?

我想用制表符替换一组连续的空格,虽然我可以用制表符替换其中一个空格,但我不知道如何去掉数组中的其余空格。我可以将“”类型转换为一个字符,这将删除它们,但是 gcc 会发出关于“从指针转换为不同大小的整数”的警告。它运行良好,但警告让我觉得有更好的方法来做到这一点。使用 '' 不起作用,因为 gcc 会抛出“空字符常量”错误。

使用空字符的示例代码

#include <stdio.h>

int main() {
char str[10] = "gay\n\0";
str[1] = '';
printf("%s", str);
}

使用空字符串的示例代码,转换为 char

#include <stdio.h>

int main() {
char str[10] = "gay\n\0";
str[1] = (char) "";
printf("%s", str);
}

第一个例子抛出错误“空字符常量”

第二个例子有效,但抛出警告“cast from pointer to integer of different size”

最佳答案

由于您决定在没有标准库函数的情况下执行此操作,因此最简单的方法是用 tab 字符 ( '\t') 是使用变量循环遍历您的字符串,以跟踪您当前是 还是 一系列空格中。

使用一个简单的 int 作为标志变量(例如 int in;)并在读取时设置 in = 1; (true)一个空格,然后每当你读到一些不是空格的东西时,设置 in = 0; (false) 来指示你在字符串中读取的当前“状态”(例如,在空格中或不在空格中)然后,当您在读取一系列空格后遇到第一个非空格字符时,只需编写一个 tab(例如,当您从 in = 1; 更改为 in = 0;).

当您遍历字符串中的每个字符时,只需保留两个指针,一个指向您当前的读取位置(例如 p),一个指向您当前的写入位置(例如 wp )。所以你有一个读指针和一个写指针。将两者都初始化为字符串的开头,然后在每次迭代时推进读取指针,如果不是空格,则写入每个字符不变,或者禁止写入所有空格字符并写入 '\t' when in1 变为 0。看到它会有所帮助。

你可以这样做:

/** spaces2tab will convert all series of spaces in s to a '\t'.
* s must be mutable (writeable) and not const or a string-literal.
*/
void spaces2tab (char *s)
{
char *p = s, /* pointer to s -- for reading */
*wp = s; /* pointer to s -- for writing */
int in = 0; /* flag -- in/out series of spaces */

while (*p) { /* loop over each char in s */
if (*p == ' ') { /* if char is ' ' set in to 'true' */
in = 1;
}
else { /* if other than space */
if (in) { /* if was series of spaces */
*wp++ = '\t'; /* write a tab */
in = 0; /* set in 'false' */
}
*wp++ = *p; /* write current char */
}
p++; /* advance to next char */
}
if (in) /* if was in series of spaces at end */
*wp++ = '\t'; /* add tab at end */

*wp = *p; /* nul-terminate at current char */
}

(注意上面的代码会将所有前导空格转换为制表符,并将所有尾随空格转换为制表符。您可以修改如果您想去除前导空格和尾随空格,则根据需要更改该行为——留给您)

花时间研究逻辑并了解它是如何工作的。最好的方法是将要转换的字符串写在一张纸上,然后在每次迭代中跟踪 pwp 点。 (没有捷径,不要在屏幕上做,拿出一张8.5x11的纸和铅笔写出来)

将要转换的字符串作为程序的第一个参数的完整示例(或者使用简单的默认字符串 "this is a test " if no argument is given)可能类似于以下(请注意编写了手册 strcpy (scopy) 以符合您将字符复制到要转换的字符串的无库函数要求):

#include <stdio.h>

#define MAXC 128

/** spaces2tab will convert all series of spaces in s to a '\t'.
* s must be mutable (writeable) and not const or a string-literal.
*/
void spaces2tab (char *s)
{
char *p = s, /* pointer to s -- for reading */
*wp = s; /* pointer to s -- for writing */
int in = 0; /* flag -- in/out series of spaces */

while (*p) { /* loop over each char in s */
if (*p == ' ') { /* if char is ' ' set in to 'true' */
in = 1;
}
else { /* if other than space */
if (in) { /* if was series of spaces */
*wp++ = '\t'; /* write a tab */
in = 0; /* set in 'false' */
}
*wp++ = *p; /* write current char */
}
p++; /* advance to next char */
}
if (in) /* if was in series of spaces at end */
*wp++ = '\t'; /* add tab at end */

*wp = *p; /* nul-terminate at current char */
}

char *scopy (char *dest, const char *src)
{
char *p = dest; /* pointer to dest */
*dest = 0; /* set dest to empty-string */

if (!src) /* valdiate src not NULL or return NULL */
return NULL;

do { /* loop copying chars from src to p */
*p++ = *src; /* (it will copy the nul-termining char) */
} while (*src++);

return dest; /* return pointer to indicate success */
}

int main (int argc, char **argv) {

char str[MAXC] = "",
*p = argc > 1 ? argv[1] : " this is a test ";

if (!scopy (str, p))
return 1;

spaces2tab (str);

printf ("original : '%s'\nconverted : '%s'\n", p, str);
}

示例使用/输出

$ ./bin/spaces2tab
original : ' this is a test '
converted : ' this is a test '

验证'\t'转换

为了验证正确的转换,通过像 hexdumpod 这样的 hex-dump 程序运行你的输出(每个操作系统都有类似的东西)

$ ./bin/spaces2tab | hexdump -Cv
00000000 6f 72 69 67 69 6e 61 6c 20 20 3a 20 27 20 20 74 |original : ' t|
00000010 68 69 73 20 20 20 69 73 20 61 20 20 20 20 20 20 |his is a |
00000020 74 65 73 74 20 20 20 20 27 0a 63 6f 6e 76 65 72 |test '.conver|
00000030 74 65 64 20 3a 20 27 09 74 68 69 73 09 69 73 09 |ted : '.this.is.|
00000040 61 09 74 65 73 74 09 27 0a |a.test.'.|
00000049

您可以确认所有系列的空格(十六进制 20)已转换为单个 tab 字符(十六进制 09)。

如果您还有其他问题,请告诉我。

关于c - 如何从 char 数组中清除单个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57838474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com