tags:

views:

106

answers:

3

Hi.

sscanf(text, "%s %s", name, company); 

parses 'ian mceknis' but it also parses 'ian   mceknis' and so on. How can i make this to parse only the first one? It must contain only one space not more.

Thank you.

+2  A: 

If you want it to reject the latter example, you'll have to roll your own function to find/reject multiple spaces.

But my guess is that what you really want to do is strip the extra spaces. See: http://stackoverflow.com/questions/122616/painless-way-to-trim-leading-trailing-whitespace-in-c

Wayne Werner
Yeah but still the string would be parse-able. This is why i can't do that.
jamall55
Then you'll absolutely have to roll your own pre-check for a double space. What about whitespace before your string? Is that allowed? What about two spaces at the end? Also, is this a homework problem? Because from a usability standpoint it's usually less desirable to restrict input options.
Wayne Werner
A: 

You can't do this using only sscanf(), which has pretty basic functionality. If you really need to enforce this (do you?), regular expressions might be more suitable here.

j_random_hacker
A: 

According to sscanf definition, this is absolutely impossible. To get desired result, I would read the text variable manually, searching for two consequent whitespace characters. If found, replace the second whitespace character with 0 and then call sscanf.

Alex Farber
Replacing the second whitespace character with '0' will just cause `company` to be set to `0mceknis` in the example given. -1.
j_random_hacker
j_random_hacker: Do you know the difference between 0 and '0' ?
Alex Farber
I wondered if you meant 0 as in the nul character (all-bits-off). In the example given, that will terminate the string before `mceknis` (i.e. the input string is now `ian ` with a single space at the end) so `sscanf()` will fail.
j_random_hacker