2011-01-13 96 views
1

我在Objective-C中有一个真正的基本命令行程序,用于搜索用户输入的信息。不幸的是,代码只会读取用户输入的一系列单词中的第一个单词。例如,如果用户输入“苹果很棒”,只有“苹果”被保留(因此稍后搜索),不包括“很棒”部分句子。如何在Objective C中搜索包含空格的字符串?

这是我到目前为止有:

char enteredQuery [128]; // array 'name' to hold the scanf string 
NSString *searchQuery; // ending NSString to hold and compare the user inputed data 

NSLog(@"Enter search query:"); 
scanf("%s", enteredQuery); //will read the next line 

searchQuery = [NSString stringWithCString: enteredQuery encoding: NSASCIIStringEncoding]; //converts scanf data into a NSString type 

我知道它有跟我使用scanf或字符编码器转换做的,但我似乎无法弄清楚。任何帮助解决这个问题非常感谢!谢谢。

回答

1

scanf为模式"%s"读取单个单词。你可以在循环中使用它。使用fgets从stdin中读取可能会更好。见herehere

相关问题