2
char string[5][5]={{'a','l','p','h','b'},{'c','d','e','f','g'},
{'i','j','k','m','n'},{'o','q','r','s','t'},
{'u','v','w','x','y'}};
char strsearch[100];
int rowindex[100];
int colindex[100];
printf("\nEnter String=");
gets(strsearch);
int iIndex,jIndex=0;
int count=0;
int row,column=0;
for(row=0;row<5;row++)
{
for(column=0;column<5;column++)
{
if(string[row][column]==strsearch[i])
{
rowindex[iIndex]=row;
colindex[jIndex]=column;
iIndex++;
jIndex++;
count++;
i++;
//printf("%d",count);
}
}
}
for(iIndex=0;iIndex<count;iIndex++)
{
printf("row=%d",rowindex[iIndex]);
printf("\ncol=%d",colindex[iIndex]);
}
我已經完成了上面的預期輸出應該如下。 輸出應該是:如何查找c中另一個字符串中字符串給定字符的索引?
Enter String=mona
row=2,3,2,0 //index of row of character 'm','o','n','a'
column=3,0,4,0 ////index of column of character 'm','o','n','a'
但它不打印任何東西。這有什麼不對?
'iIndex'沒有在第一個循環中初始化,所以你的程序可能會崩潰。 – dasblinkenlight
[不要使用不安全的gets()函數](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-notbe -used) –
'我'未申報。 – BLUEPIXY