gpt4 book ai didi

c - 在c中使用原型(prototype)将一个字符替换为另一个字符

转载 作者:行者123 更新时间:2023-11-30 20:21:17 25 4
gpt4 key购买 nike

#include "stdafx.h"
#include "stdio.h"
void repl(char *string, char oldc, char newc);

char text[60] = { "I am going to replace the character a with the character i";
char newc = 'b';
char oldc = 'a';

int main()
{
void repl(char *string, char oldc, char newc);

return 0;

}

void repl(char *string, char oldc, char newc)
{
int i = 0;
for (i = 0; i < *string; i++)
{
if (*(string + i) == oldc)
{
*(string + i) == newc;
}
}
printf("%s", string);
}

我正在尝试将字符 a 替换为字符 b。我知道如何在不使用指针的情况下做到这一点,但对于指针我不太确定。我得到的原型(prototype)是:

void repl(char *string, char oldc, char newc);

最佳答案

这就是你所追求的。我强烈建议您阅读一些基本的 C 编程主题,例如调用函数和声明 char 数组。与此同时...

#include "stdafx.h"
#include "stdio.h"
void repl(char *string, char oldc, char newc);

char text[60] = "I am going to replace the character a with the character i";
char newc = 'i';
char oldc = 'a';

int main()
{
repl(text, oldc, newc);
return 0;
}

void repl(char *string, char oldc, char newc)
{
int i = 0;
for (i = 0; string[i]; i++)
{
if (string[i] == oldc)
{
string[i] = newc;
}
}
printf("%s", string);
}

关于c - 在c中使用原型(prototype)将一个字符替换为另一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43294707/

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