首页 > 解决方案 > 如何从字符串中获取字符

问题描述

我可以问为什么它显示“零”而不是索引吗?

电流输出:0

预期输出:6

#include<stdio.h>
#include<stdlib.h>
int main() {
    int index_equal;    
    char array[20] = {"2n+n2n=n4n"};
    for (int j = 0; j > 20; j++) {
        if (array[j] == '=') {
            index_equal = j;
        }
    }
    printf("\n%d", index_equal);
}

标签: carraysstring

解决方案


for 循环有问题你应该写 j<20 否则循环将永远不会执行

for(int j=0;j<20;j++)

这应该是正确的方法


推荐阅读