我在C中有一个字符串。我想获取字符串中该字符的所有实例的索引位置。
它出现不止一次,所以我需要返回所有字符的索引。
示例是包含str1
的字符串Banana
。角色' a'在索引位置找到:1,3,5
这样做的最佳方式是什么?
答案 0 :(得分:2)
为什么不这样呢?
for (i = 0; i < strlen(str); i++)
if (str[i] == ch)
location[index++] = i;
答案 1 :(得分:0)