11

I want the user to input a string and then assign the input to an NSString. Right now my code looks like this:

NSString *word; 

scanf("%s", &word);
Omar
  • 2,155
  • 4
  • 24
  • 38

8 Answers8

8

The scanf function reads into a C string (actually an array of char), like this:

char word[40];

int nChars = scanf("%39s", word);   // read up to 39 chars (leave room for NUL)

You can convert a char array into NSString like this:

NSString* word2 = [NSString stringWithBytes:word 
                                     length:nChars
                                   encoding:NSUTF8StringEncoding];

However scanf only works with console (command line) programs. If you're trying to get input on a Mac or iOS device then scanf is not what you want to use to get user input.

progrmr
  • 75,956
  • 16
  • 112
  • 147
  • Does `scanf` work with `char*`? I don't want to limit the input using a fixed char array. – raffian Sep 04 '12 at 02:09
  • @SAFX: yes, but there are risks to using scanf, see [details here](http://stackoverflow.com/q/2430303/154803). That's why I used a fixed size buffer and limited the length in scanf. – progrmr Sep 17 '12 at 21:26
  • @progrmr Perhaps this is outmoded? I'm getting 'No know class method' for the stringWithBytes call. – sunny Jul 20 '15 at 22:13
  • I am new to Objective C and the Mac/iOS development. I writing a basic console application and wanted to use scanf with NSString and arrived at this post. I used the above method "stringWithBytes" but I got warnings that the object may not support the message. Though it was just a compile time warning, I searched more an found this "**stringWithUTF8String**". u can use it as a message as static methods of NSString or u can use it as "**initWithUTF8String**" along with alloc and get the NSString object from the character array. – Amogh Talpallikar Dec 10 '11 at 14:38
  • @sunny use [NSString stringWithCString:word encoding:NSASCIIStringEncoding] instead – aj_ios Jul 06 '20 at 18:46
7

scanf does not work with NSString as scanf doesn’t work on objects. It works only on primitive datatypes such as:

  1. int
  2. float
  3. BOOL
  4. char

What to do?

Technically a string is made up of a sequence of individual characters. So to accept string input, you can read in the sequence of characters and convert it to a string.

use:

[NSString stringWithCString:cstring encoding:1];

Here is a working example:

NSLog(@"What is the first name?");
char cstring[40];
scanf("%s", cstring);

firstName = [NSString stringWithCString:cstring encoding:1];

Here’s an explanation of the above code, comment by comment:

  1. You declare a variable called cstring to hold 40 characters.
  2. You then tell scanf to expect a list of characters by using the %s format specifier.
  3. Finally, you create an NSString object from the list of characters that were read in.

Run your project; if you enter a word and hit Enter, the program should print out the same word you typed. Just make sure the word is less than 40 characters; if you enter more, you might cause the program to crash — you are welcome to test that out yourself! :]

Taken from: RW.

MarkP
  • 2,546
  • 5
  • 31
  • 48
7

scanf does not work with any object types. If you have a C string and want to create an NSString from it, use -[NSString initWithBytes:length:encoding:].

Chuck
  • 234,037
  • 30
  • 302
  • 389
4

This is how I'd do it:

char word [40];
scanf("%s",word);

NSString * userInput = [[NSString alloc] initWithCString: word encoding: NSUTF8StringEncoding];
W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • NOTE: You don't need to put '&' Since word isn't a pointer, and is the actual character array. – ManOx Oct 30 '12 at 17:19
1

Maybe this will work for you because it accepts string with spaces as well.

 NSLog(@"Enter The Name Of State");
 char name[20];
 gets(name);
 NSLog(@"%s",name);
sagar
  • 379
  • 4
  • 13
1

yes, but sscanf does, and may be a good solution for complex NSString parsing.

0

Simple Solution is

char word[40];
scanf("%39s", word);
NSString* word2 = [NSString stringWithUTF8String:word];
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
singh.jitendra
  • 490
  • 1
  • 9
  • 19
0

The NSFileHandle class is an object-oriented wrapper for a file descriptor. For files, you can read, write, and seek within the file.

NSFileHandle *inputFile = [NSFileHandle fileHandleWithStandardInput];
NSData *inputData = [inputFile availableData];
NSString *word = [[NSString alloc]initWithData:inputData encoding:NSUTF8StringEncoding];
Jayce
  • 427
  • 5
  • 6