2

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.

interjay
  • 107,303
  • 21
  • 270
  • 254
jamall55
  • 127
  • 1
  • 3
  • 10
  • Can you clarify your question? It parses X but it also parses X? Can you give a more specific example? – Vicky May 24 '10 at 15:12
  • It's hard to see what you're asking since your two parse string examples are the same. Give an example case of what doesn't work. – Michael Chinen May 24 '10 at 15:13
  • i just want to parse 'ian mceknis blablabla' 'ian mceknis'. But when the string contains more space the parse would still be valid. But i don't want this. It must parse only and only 'ian mceknis' with the specified spaces. 'ian mceknis blablabla' shouldn't be valid. I hope you get it what i am trying to say. Sorry. – jamall55 May 24 '10 at 15:23

4 Answers4

2

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: How do I trim leading/trailing whitespace in a standard way?

Community
  • 1
  • 1
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • Yeah but still the string would be parse-able. This is why i can't do that. – jamall55 May 24 '10 at 15:38
  • 1
    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 May 24 '10 at 19:30
0

its not the cleanest, but this is how i did it for a project. Basically rather than having a space in the formatted string, it takes up to two spaces(you can change it to also deal with tabs or whatever). If there is only one space then the second char is still null, and if its not null then it was overwritten.

So I would change:

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

to:

char space[2] = "\0\0"
sscanf(text, "%s%2[ ]%s", name, space, company);
if(space[1] != '\0') {
    //there was an extra space, handle it however
}
0

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
  • 50,331
  • 10
  • 105
  • 169
-1

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 F
  • 42,307
  • 41
  • 144
  • 212
  • Replacing the second whitespace character with '0' will just cause `company` to be set to `0mceknis` in the example given. -1. – j_random_hacker May 24 '10 at 15:32
  • j_random_hacker: Do you know the difference between 0 and '0' ? – Alex F May 25 '10 at 08:24
  • 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 May 25 '10 at 10:32