How to read input in Objective-C? -
i trying write simple code searches 2 dictionaries string , prints console if string appears in both dictionaries. want user able input string via console, , pass string variable message. wondering how go getting
string console , using argument in following method call.
[x rangeofstring:"the string goes here" options:nscaseinsensitivesearch];
i unsure how string user. use scanf(), or fgets(), char , convert nssstring, or scan nsstring itself. wondering how pass string argument. please help:
here code have far. know not succinct, want job done:
#import <foundation/foundation.h> #include <stdio.h> #include "stdlib.h" int main(int argc, const char* argv[]){ @autoreleasepool { char *name[100]; printf("please enter name wish search for"); scanf("%s", *name); nsstring *name2 = [nsstring stringwithformat:@"%s" , *name]; nsstring *namestring = [nsstring stringwithcontentsoffile:@"/usr/share/dict/propernames" encoding:nsutf8stringencoding error:null]; nsstring *dictionary = [nsstring stringwithcontentsoffile:@"/usr/share/dict/words" encoding:nsutf8stringencoding error:null]; nsarray *namestring2 = [namestring componentsseparatedbystring:@"\n"]; nsarray *dictionary2 = [dictionary componentsseparatedbystring:@"\n"]; int nsyes = 0; int dictyes = 0; (nsstring *n in namestring2) { nsrange r = [n rangeofstring:name2 options:nscaseinsensitivesearch]; if (r.location != nsnotfound){ nsyes = 1; } } (nsstring *x in dictionary2) { nsrange l = [x rangeofstring:name2 options:nscaseinsensitivesearch]; if (l.location != nsnotfound){ dictyes = 1; } } if (dictyes && nsyes){ nslog(@"glen appears in both dictionaries"); } } }
thanks.
safely reading standard input in interactive manner in c kind of involved. standard functions require fixed-size buffer, means either input long (and corrupt memory!) or you'll have read in loop. , unfortunately, cocoa doesn't offer whole lot of help.
for reading standard input entirely (as in, if you're expecting input file on standard input), there nsfilehandle, makes pretty succinct. interactively reading , writing want here, pretty have go linked answer reading.
once have read input c string, can turn nsstring with, example, +[nsstring stringwithutf8string:]
.
Comments
Post a Comment