多维数组的用途是将小写和大写字母都存储在一个数组中,以便它们可以被映射。一种有效的方法是使用他们的ASCII
代码,但是由于您是初学者,我想这个示例将向您介绍如何处理for loops
and multidimensional arrays
,我认为这也是讲师的计划。
让我们首先为字母设置数组。我们将有两行,每行有 26 个字母:
alphabetsEnglish[26][2] = {{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}};
现在我们可以映射这两种情况的元素。
int main()
{
int c,i,j;
char word[10];
printf("Enter a word:");
scanf("%s",word);
c=strlen(word);
printf("Your word has %d letters ", c);
for (i = 0; i < c; i++) //loop for the length of your word
{
for (j = 0; j <= 25; j++) //second loop to go through your alphabet list
{
if (word[i] == alphabetsEnglish[0][j] || word[i] == alphabetsEnglish[1][j]) //check for both cases of your alphabet
{
printf("Your alphabet %c translates to %d: ", word[i], j);
}
}
}
return 0;
}