我尝试扫描一个字符串并将其划分为单独的字符串。我的代码是:
#import <Foundation/Foundation.h>
NSArray *wordsArray;
int amount;
char input[80];
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSString *stra=[[NSString alloc] initWithString:@""];
// stra=@"this is my sentence";
NSLog(@"Please Enter a sentence!");
scanf("%s",&input);
stra=[NSString stringWithCString:input encoding:NSASCIIStringEncoding];
// NSLog(@"words: %@",str);
wordsArray = [stra componentsSeparatedByString:@" "];
amount= [wordsArray count];
NSLog(@"Number of Shows : %d", amount);
for (int i=0; i<amount; i++)
{
NSString *subString = [wordsArray objectAtIndex:i];
NSLog(@"\n %@",[subString uppercaseString]);
}
}
return 0;
}
输入时:“Test1 test2 test3”
我得到:测试1
该代码不考虑空格。如何扫描大厅字符串?