2

I am trying to write some simple code that searches two dictionaries for a string and prints to the console if the string appears in both dictionaries. I want the user to be able to input the string via the console, and then pass the string as a variable into a message. I was wondering how I could go about getting a string from the console and using it as the argument in the following method call.

[x rangeOfString:"the string goes here" options:NSCaseInsensitiveSearch];

I am unsure as to how to get the string from the user. Do I use scanf(), or fgets(), into a char and then convert it into a NSSstring, or simply scan into an NSString itself. I am then wondering how to pass that string as an argument. Please help:

Here is the code I have so far. I know it is not succinct, but I just want to get the 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 the name you wish to 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;

        for (NSString *n in nameString2) {

            NSRange r = [n rangeOfString:name2 options:NSCaseInsensitiveSearch];

            if (r.location != NSNotFound){
                nsYES = 1;
            }
        }
        for (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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
theideasmith
  • 2,835
  • 2
  • 13
  • 20

1 Answers1

0

Safely reading from standard input in an interactive manner in C is kind of involved. The standard functions require a fixed-size buffer, which means either some input will be too long (and corrupt your memory!) or you'll have to read in a loop. And unfortunately, Cocoa doesn't offer us a whole lot of help.

For reading standard input entirely (as in, if you're expecting an input file over standard input), there is NSFileHandle, which makes it pretty succinct. But for interactively reading and writing like you want to do here, you pretty much have to go with the linked answer for reading.

Once you have read some input into a C string, you can easily turn it into an NSString with, for example, +[NSString stringWithUTF8String:].

Community
  • 1
  • 1
Chuck
  • 234,037
  • 30
  • 302
  • 389