0
#include <stdio.h>
#include <string.h.>
int main()
{
char hurray[] = "Hooray for all of us"; //Character String
char *hurrayptr = hurray; //Pinter to array hurray
int i = 0; //Used in for loop to display position and character
int d = 0; //Used in printf statement to count the position
int k;
int count = 0;
int index;
int f = 0;
printf("\tPosition\t Character");
while (hurray[i] > 20) {
for (i = 0; i < 20; i++) {
printf("\n\t hurray[%d]\t\t %c", d++, *hurrayptr++);
}
}
for (k = 0; hurray[k]!= '\0'; k++)
if ('a' == hurray[k]) { //specifies character 'a' is to be counted
count++;
}
printf("\n'A' occurs %d times in this array\n", count);
hurrayptr = strchr(hurray, 'a');
index = (int)(hurrayptr - hurray);
f++;
printf("The letter 'a' was find in hurray[%d]\n", index);
return 0;
}
我試圖使它顯示數組hurray []中的元素數,然後它查找在數組內發生了多少次'a'。然後我需要找到找到的'a'的索引。我只能在它停止之後找到第一個'a'。我該如何解決?查找數組中的字符'a'的索引
什麼是'while(hurray [i]> 20){'試圖做什麼? – chux
循環printf語句20次以顯示數組中的字符及其索引 – bobblehead808
您有語法錯誤。嘗試將其編輯爲可編譯的第一個東西。 –