gpt4 book ai didi

c - 如何索引: in C?

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

#include <stdio.h>
#include <string.h>


int main() {

char a[] = "1234:once:echo hello";
int i = 0;
while(i < strlen(a)) {
if(a[i] == ':') {
break;
}
i++;
}
a = a + i;
printf("%s\n", a);
}

这个程序只是遍历字符串 a 直到它命中 ':',然后我想让 a 变成“:once:echo hello”,我尝试通过添加 i 来做到这一点。不确定为什么它不起作用我记得过去有类似的工作。

最佳答案

如果我理解你只是想浏览你的文本并找到第一个 ':' 然后打印从那一点到最后的 a 的剩余部分,你可以使用循环生成的索引很容易地做到这一点,例如

#include <stdio.h>
#include <string.h>

int main( void) {

char a[] = "1234:once:echo hello";
int i = 0;

while (a[i]) { /* loop over each char */
if (a[i] == ':') /* 1st semi-colon found */
break; /* exit loop */
i++; /* increment index */
}
printf ("%s\n", a + i); /* output string from index */
}

示例使用/输出

$ ./bin/walkarray
:once:echo hello

关于c - 如何索引: in C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377469/

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