C语言 C 替换字符数组中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16573206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C replace char in char array
提问by Cmag
Folks, need to search through a character array and replace any occurrence of '+','/',or'=' with '%2B','%2F', and '%2F' respectively
伙计们,需要在字符数组中搜索并分别用 '%2B'、'%2F' 和 '%2F' 替换任何出现的 '+'、'/' 或'='
base64output variable looks like
base64output 变量看起来像
FtCPpza+Z0FASDFvfgtoCZg5zRI=
code
代码
char *signature = replace_char(base64output, "+", "%2B");
signature = replace_char(signature, "/", "%2F");
signature = replace_char(signature, "=", "%3B");
char replace_char (char *s, char find, char replace) {
while (*s != 0) {
if (*s == find)
*s = replace;
s++;
}
return s;
}
(Errors out with)
(错误与)
s.c:266: warning: initialization makes pointer from integer without a cast
What am i doing wrong? Thanks!
我究竟做错了什么?谢谢!
回答by yoones
If the issue is that you have garbage in your signature variable:
如果问题是您的签名变量中有垃圾:
void replace_char(...)is incompatible with signature = replace_char(...)
void replace_char(...)不兼容 signature = replace_char(...)
Edit:
编辑:
Oh I didn't see... This is not going to work since you're trying to replace a char by an array of chars with no memory allocation whatsoever.
哦,我没有看到......这将不起作用,因为您试图用没有任何内存分配的字符数组替换字符。
You need to allocate a new memory chunk (malloc) big enough to hold the new string, then copy the source 's' to the destination, replacing 'c' by 'replace' when needed.
您需要分配一个足够大的新内存块 (malloc) 以容纳新字符串,然后将源 's' 复制到目标,在需要时将 'c' 替换为 'replace'。
The prototype should be:
char *replace_char(char *s, char c, char *replace);
原型应该是:
char *replace_char(char *s, char c, char *replace);
回答by asveikau
You could go for some length discussing various ways to do this.
你可以花一些时间讨论各种方法来做到这一点。
Replacing a single char is simple - loop through, if match, replace old with new, etc.
替换单个字符很简单 - 循环,如果匹配,用新替换旧等。
The problem here is that the length of the "new" part is longer than the length of the old one.
这里的问题是“新”部分的长度比旧部分的长度长。
One way would be to determine the length of the new string (by counting chars), and either (1) try to do it in place, or (2) allocate a new string.
一种方法是确定新字符串的长度(通过计算字符数),然后(1)尝试就地进行,或(2)分配一个新字符串。
Here's an idea for #1:
这是#1的想法:
int replace(char *buffer, size_t size, char old, const char *newstring)
{
size_t newlen = strlen(newstring);
char *p, *q;
size_t targetlen = 0;
// First get the final length
//
p = buffer;
while (*p)
{
if (*p == old)
targetlen += newlen;
else
targetlen++;
++p;
}
// Account for null terminator
//
targetlen++;
// Make sure there's enough space
//
if (targetlen > size)
return -1;
// Now we copy characters. We'll start at the end and
// work our way backwards.
//
p = buffer + strlen(buffer);
q = buffer + targetlen;
while (targetlen)
{
if (*p == old)
{
q -= newlen;
memcpy(q, newstring, newlen);
targetlen -= newlen;
--p;
}
else
{
*--q = *p--;
--targetlen;
}
}
return 0;
}
Then you could use it this way (here's a quick test I did):
然后你可以这样使用它(这是我做的一个快速测试):
char buf[4096] = "hello world";
if (replace(buf, sizeof(buf), 'o', "oooo"))
{
fprintf(stderr, "Not enough space\n");
}
else
{
puts(buf);
}
回答by Joe DF
1.
1.
for
charuse''single quotesfor
char*use""double quotes
对于
char使用''单引号对于
char*使用""双引号
2.
2.
- The function does include the
returnkeyword, therefore it does not return what you'd expect
- 该函数确实包含
return关键字,因此它不会返回您期望的内容
3.
3.
These webpages have examples on string replacement
回答by BLUEPIXY
fix like this
像这样修复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *replace_char (char *str, char find, char *replace) {
char *ret=str;
char *wk, *s;
wk = s = strdup(str);
while (*s != 0) {
if (*s == find){
while(*replace)
*str++ = *replace++;
++s;
} else
*str++ = *s++;
}
*str = 'void replace_char (char *s, char find, char replace)
';
free(wk);
return ret;
}
int main(void){
char base64output[4096] = "FtCPpza+Z0FASDFvfgtoCZg5zRI=";
char *signature = replace_char(base64output, '+', "%2B");
signature = replace_char(signature, '/', "%2F");
signature = replace_char(signature, '=', "%3B");
printf("%s\n", base64output);
return 0;
}
回答by stdcall
your replace_charsignature returns void
你的replace_char签名回来了void
signature = replace_char(signature, "=", '%3B');
But, when the linker tries to resolve the following
但是,当链接器尝试解决以下问题时
char * replace_char(char * input, char find, char replace)
{
char * output = (char*)malloc(strlen(input));
for (int i = 0; i < strlen(input); i++)
{
if (input[i] == find) output[i] = replace;
else output[i] = input[i];
}
output[strlen(input)] = '##代码##';
return output;
}
It doesn't find any function that's called replace_charand returns int (int is the default if there's no prototype).
它没有找到任何被调用replace_char并返回 int 的函数(如果没有原型,int 是默认值)。
Change the replace_charfunction prototype to match the statement.
更改replace_char函数原型以匹配语句。
EDIT:The warning states that your function returns char, but you use it as a char *also, your function doesn't return anything, do you need to return something ?
It looks like you don't really understand the code that you're working with.
Fixing errors and warnings without understanding exactly what you need to do is worthless..
编辑:警告指出您的函数返回char,但您也将其用作char *,您的函数不返回任何内容,您是否需要返回某些内容?看起来您并不真正理解您正在使用的代码。在不完全了解您需要做什么的情况下修复错误和警告是毫无价值的。
回答by Ammar Hourani
below is a code that ACTUALLY WORKS !!!!
下面是一个实际工作的代码!!!!
Ammar Hourani
阿马尔·胡拉尼
##代码##
