gpt4 book ai didi

c - 通过 args 读取字符串

转载 作者:行者123 更新时间:2023-11-30 17:56:49 24 4
gpt4 key购买 nike

嗨,

我正在尝试通过 args[] 参数将字符串读入我的代码中,就像我在 Java 中所做的那样。基本上,这就是我想做的:

 - read the String "machine" over launch-parameter
- go through every letter of that string in a loop
- while in the loop, check is current letter equals "e"
- if letter equals "e", replace it with "a"
- return edited string

这是向 C 表达我的基本问题的最佳方式。因此,如果您不冒犯这篇文章,我会很高兴。

我如何实现该代码?

最佳答案

这里有一个(几乎)不涉及指针的解决方案,但如果您打算进行中等高级的 C 编程,您应该真正了解指针。

void replace_e_with_a(char str[])
{
int i, len = strlen(str);
for (i=0; i<len; i++) {
if (str[i] == e) str[i] = a;
}
}

int main(int argc, char *argv[]){
int i;
for (i = 1; i < argc; i++) {
replace_e_with_a(argv[i]);
puts(argv[i]);
}
}

关于c - 通过 args 读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219054/

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