#include <iostream>
#include <string>
using namespace std;
int ABReturn_value(string a);
int main(int argc,char *argv[]){
cout<<ABReturn_value("a")<<endl;
return 0;
}
int ABReturn_value(string a){
//declare an internal character array containing the alphabet
char alphabet[2][26] = {{'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'}};
//variable to hold the acrued value
int value=0;
//look through the provided string's characters and acrue the value based on which address in the refernece array the letter corresponds to
for(unsigned int x = 0;x<=(a.size())-1;x++)
for(int y=0;y<=26-1;y++){
if(a[x]==alphabet[1][y]||a[x]==alphabet[2][y]){
value+=y;
}
}
return value;
}
我遇到的问题是基于字符的这个测试输入ai我期望函数返回值1但是它返回40,我一直在盯着它看起来无法弄清楚我做了什么错了所以也许是我遗失的东西?提前谢谢!
答案 0 :(得分:0)
@immibis已经回答了这个问题,你能否将其标记为已回答?
字母[1]应为字母[0],字母[2]应为字母[1] - immibis