【发布时间】:2018-02-04 07:16:36
【问题描述】:
我以前从未做过Objective-C,只会一点点c和c++。为了好玩,我做了一个小的 c++ 程序,不是最有效的代码,但没关系,它将一个单词/句子转换为一个单词/句子,所有辅音都在末尾添加 op。
char vowels[]{'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','q','w','x','y','z'};
string word;
int temp;
cout << "Enter a word or exit to stop program: " << endl;
getline(cin,word);
cout << "OP translation is: ";
for(int i = 0; word[i] != '\0'; i++){
if(word[i] == vowels[0] || word[i] == vowels[1] || word[i] == vowels[2] || word[i] == vowels[3] || word[i] == vowels[4] || word[i] == vowels[5] || word[i] == vowels[6]
|| word[i] == vowels[7] || word[i] == vowels[8] || word[i] == vowels[9] || word[i] == vowels[10] || word[i] == vowels[11] || word[i] == vowels[12] || word[i] == vowels[13]
|| word[i] == vowels[14] || word[i] == vowels[15] || word[i] == vowels[16] || word[i] == vowels[17] || word[i] == vowels[18] || word[i] == vowels[19] || word[i] == vowels[20]
|| word[i] == vowels[21]){
if(word[0])
cout<<word[i] << "op" << " ";
else
cout << " " << word[i] << "op";
}
else
cout<<word[i] << " ";
}
现在我想尝试将它变成一个应用程序,但我不确定如何在objective-c 中循环遍历字符串和数组。很多可能是错误的,但从一个小教程中我发现了这一点。
NSString *word = self.translateTextField.text;
NSArray *vowels;
vowels = [NSArray arrayWithObjects: @"b",@"c",@"d",@"f",@"g",@"h",@"j",@"k",@"l",@"m",@"n",@"p",@"q",@"r",@"s",@"t",@"v",@"q",@"w",@"x",@"y",@"z", nil];
NSArray *compArray = [word componentsSeparatedByString:@"-"];
int i;
for (i = 0; self.translateTextField.text != '\0'; i++){
//NSLog (@"Element %i = %@", i, [vowels objectAtIndex: i]);
if([compArray objectAtIndex:i] == [vowels objectAtIndex:i]){
self.translationTextField.text = ;
}
else
self.translationTextField.text = ;
}
基本上,如果您将 home 写入 translateTextField 并点击 translatePressed 按钮,我希望它在 translationTextField 中输出 hop o mop e。
【问题讨论】:
标签: c++ ios objective-c arrays xcode