从C中的char *数组中删除空格 [英] remove spaces from char* array in C
问题描述
我正在为游戏模拟器使用 C
中的插件,在检查输入消息是否包含任何网站URL之前,我想从聊天消息中删除所有空格.
I am working on a plugin in C
for a game emulator, where I would like to remove all spaces from chat message before checking if the input messages contain any website urls.
所以,我有这样的功能来消除空白(从输入消息中删除空格):
So, I have this function like this to deblank (remove spaces from input message):
char* deblank(char* input) {
int i, j;
char *output = input;
for (i = 0, j = 0; i < strlen(input); i++, j++) {
if (input[i] != ' ')
output[j] = input[i];
else
j--;
}
output[j] = 0;
return output;
}
我在插件钩子中这样使用它:
I use it in my plugin hook like this:
bool my_pc_process_chat_message(bool retVal___, struct map_session_data *sd, const char *message) {
if (retVal___ == true) {
char* check_message = deblank(message);
bool url_detected = (stristr(check_message, "://") || stristr(check_message, "www.") || stristr(check_message, "vvvvvv.") || stristr(check_message, "wvvw."));
if (!url_detected) {
int len = sizeof(tld_list) / sizeof(tld_list[0]);
int i;
for (i = 0; i < len; ++i) {
if (stristr(check_message, tld_list[i])) {
url_detected = true;
break;
}
}
}
if (url_detected) {
clif->messagecolor_self(sd->fd, COLOR_RED, "Possible GM Impersonation Detected - you cannot type website URLs in chat.");
return false;
}
}
return retVal___;
}
以上方法有效,但有副作用.似乎正在发生的事情是,当输入消息不包含任何网站网址时,输入消息将被修改,并且所有消息中的空格都将被删除.
The above works, but there's a side-effect. What seems to be happening is that the input message is getting modified and all spaces from the message being removed, when it doesn't contain any website urls.
例如,如果我输入:
转到www.some-domain.com
Go to www.some-domain.com
上面的内容被阻止,所以这样做(当玩家尝试变得更聪明并使用空格时):
The above get's blocked, So does this (when players try to be clever and use spaces):
w w w w.S o e e-d o m a n n.c o m
w w w . s o m e - d o m a i n . c o m
但是,当您键入不包含任何网站网址的普通消息时,
However, when you type a normal message that doesn't contain any website urls like this:
你好,你好吗?
它变成了以下内容:
你好,你好吗?
这是因为以下行: char * check_message = deblank(message);
删除功能似乎正在修改原始消息.我以为该函数将返回原始消息的新副本,不带空格.
This is because of this line: char* check_message = deblank(message);
The deblank function appears to be modifying the original message. I thought the function would return a new copy of the orignal message, without spaces.
在检查之前,有没有办法我可以临时复制原始 input消息
,并从中删除空格?
Is there a way I can make a temporary copy of the original input message
, remove spaces from that, before doing my check?
此外,如何清除分配给原始输入的临时副本的内存?我不希望这个问题持续存在.还是在代码完成作用域的执行后,C会自动清除该临时字符数组?
Also, how do you clear allocated memory to the temp copy of the orignal input? I don't want this to linger around. Or does C automatically clears that temp char array once the code finishes it's execution in the scope?
推荐答案
不,您实际上不能这样做.
No, you can't actually do that.
有两种解决方案:-第一次循环字符串以计算有多少个非空格字符,这样您就知道需要分配多少.
There are two solutions: - Loop the string a first time to count how many non-space characters there are, so you know how much you need to allocate.
int count_not_empty(const char *str)
{
int n = 0;
for (; *str; ++str) {
if (*str != ' ') n++;
}
return n + 1; // the +1 is for the nulbyte at the end
}
- 分配一个与要修剪的字符串一样大的字符串,并在末尾填充nbytes.
即
char *copy_not_empty(const char *str)
{
size_t i;
size_t j;
char *new = malloc(strlen(str) + 1);
for (i = j = 0; i < strlen(str); ++i)
{
if (str[i] != ' ')
new[j++] = str[i];
}
for (; j <= i; j++)
new[j] = 0; // pad with nulbytes
return new;
}
这篇关于从C中的char *数组中删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!