Reputation: 21
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDictionary * ProbablyInaccurateFinnishtoEnglshtranslation= [NSDictionary dictionaryWithObjectsAndKeys: @"postilaatikko", @"mailbox", @"oppikirja", @"textbook", @"näppäimistö", @"keyboard", @"piano", @"piano", @"laskukone", @"calculator", @"manteli", @"almond", @"lumi", @"snow", @"vuori", @"mountain", @"aika", @"time", @"kynttilä", @"candle", nil];
NSString * inputSentence;
char cstring[451];
NSArray * sentenceIntoWords;
NSMutableString * translatedSentence;
int i = 0;
NSLog(@"Enter a sentence: \n");
//scanf("%s", &cstring);
gets (cstring);
inputSentence = [NSString stringWithCString: cstring encoding: NSASCIIStringEncoding];
sentenceIntoWords = [inputSentence componentsSeparatedByString: @" "];
for(i=0; i<[sentenceIntoWords count]; i++)
{
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i] == nil])
{
translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]];
}
else {
translatedSentence= [ProbablyInaccurateFinnishtoEnglshtranslation objectForKey: [sentenceIntoWords objectAtIndex: i]];
}
}
NSLog(@"%@", translatedSentence);
[pool drain];
return 0;
}
What I'm trying to do is compare each word of a sentence entered by the user with the NSArray, replacing the key with the translated word when matching. Ok so I've changed it to this: if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]]) { translatedSentence= [ProbablyInaccurateFinnishtoEnglshtranslation objectForKey: [sentenceIntoWords objectAtIndex: i]];
}
else {
translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]];
}
Now only the last word of the input sentence shows up, is there error in my logic?
Upvotes: 2
Views: 1019
Reputation: 1
Similar error it is showing in my program, i.e.passing argument 1 of ‘strlen’ makes pointer from integer without a cast
//Program to read the fasta sequence and randomize it!
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int main(int ac,char *av[])
{
int i;
int l1;
FILE *file1;
if ( ac < 2 ) return 0;
file1= fopen( av[1] , "r" ); //"r" is a string and 'r' is a character
char x;
while(((x=fgetc(file1))!= EOF) && (x!='\n')); //skip the header part
while(((x=fgetc(file1))!= EOF) && (x!='>'))
{
if(isalpha(x)) //reading all the alphabets after this(>) sign
{
printf("%c",x);
}
}
//To calculate the length of sequence
l1=strlen(x);
printf ("The sequence is %d characters long.\n",l1);
//to Randomize all the characters present in a string(FASTA sequence)
srand(time(NULL));
for (i=l1-1;i>0;i--)
{
char j = rand() % (i+1);
char temp = j;
j = x;
x = temp;
}
fclose(file1);
return 0;
}
Upvotes: -1
Reputation: 22930
you can not use primitive types with objectForKey:
.
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i] == nil])
[sentenceIntoWords objectAtIndex:i] == nil
line will give you boolean output
so this is wrong if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:boolean_value(yes/NO))
you should do like this
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]] == nil)
Upvotes: 0
Reputation: 410942
Bracket's in the wrong place. You want this:
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]] == nil)
What you have now is roughly equivalent to this:
BOOL isNil = [sentenceIntoWords objectAtIndex:i]] == nil;
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:isNil)
Obviously that's wrong, as objectForKey
is (most likely) expecting an object pointer, not a boolean value.
Upvotes: 2