0

I try to scan a string and divide it to separate strings. My code is:

#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;
}

On input: "Test1 test2 test3"

I get: Test1

The code doesn't consider spaces. How do i scan the hall string?

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Ilazar1x
  • 59
  • 11

1 Answers1

1

A reason this isn't working is that scanf doesn't have a size of string to look for, so it is stopping at the first whitespace. Another method you can use is fgets() for the input.
This link here provides more information (I know it says c but you are using c functions here) Allowing white spaces c

Community
  • 1
  • 1
CBredlow
  • 2,790
  • 2
  • 28
  • 47