-
text comparing
Hello,
I am a begginer in C programming, an I have a problem I can't fix.
The problem is that I have a "string" (array of char) and I want to know if it has a specific form. That means, for example, I want to know if it has tree numbres, a space and four lettres.
Does anyone know how I can do it?
Thank you very much and excuse my very bad English. If you don't understand something please tell me and I will try to explain it better.
Thanks a lot.
-
use isdigit and isalpha functions
-
-
Thank you both.
I can't use sscanf because the text I have to compare doesn't come form the keyboard, it is the return of an other function (an OCR).
Using isdigit and isalpha is good but I an affraid it will take too long. The text I have to compare is 10 caraters long and it have 3 possibilities. So, is there an other way to do it with the wole text instead of doing it letter by letter?
Thanks a lot.
-
sscanf has nothing to do with keyboard - it scans text buffers
-
Excuse me, I think I confused with scanf or something like that.
I apologise but I am realy new in C programming, I always use Java.
I will investigate sscanf.
Thanks a lot.
-
> The text I have to compare is 10 [characters] long. [...] Using isdigit and isalpha is good but I an affraid it will take too long.
Not really. In fact if you do it correctly you won't miss the time.
-
Ok I tried with sscanf but I doesn't work properly (maybe I'm not using it well). If I understand how it works, before using it, I have to know which kind of characters I have. That means that I have to know how many letters and numbers I have an in which order they come. But it is not the case. My problem is that I don't know what I have. Maybe it is easyer by un example.
Imagine that I have to read the plates of the cars in a parking. I have to say how many french cars I have, how many belgians, how many spanish and how many others. And I have to do it as fast as I can because I print in the ticket the plate with a small picture for each nationality (It is only a stupid example).
That's the deal.
Maybe the answer is with isdigit and isanal. I will try it. But I am affraid that it will take long time to recognise the nationality (not me to do it :-), but the program to work).
Thanks a lot.
-
with sscanf you should build as many format strings as you expect different formats,
when you sscanf your input with it and check the number of successfully parsed values.
If it is equal to the total number of tokens in this format - you probably found what you wanted
you will probably use formats like
"%2[0-9]%3[A-Z]" or something
-
Thanks a lot, it works very well.
I was putting %s and things like that, so it always read something, but with the last change everything works ok.
If someone else has the same problem, you only have to put your sscanf in an if() to check if the format entered is the same as in the pattern.
Again thanks a lot.
-
Sorry it's me again.
I am finding troubles. If I said that the text has to be "%2[A-Z] %3[0-9]" and you enter something like AAAAAA 2222222 it sais that it is OK, but it isn't the same, it has to be exactly the same. I think that maybe I have to read the size of the output of the sscanf. Is there any other solution.
Thanks again.
-
Code:
char temp1[3];
char temp2[4];
int ret = sscanf("AAAAAA 2222222","%2[A-Z] %3[0-9]",temp1,temp2);
ret is 1 so it is not the same
PS. and don't dorget to add %n at the end and check that all characters are read
-
if you want it to give you a result as fast as possible, you want to layer your tests from fastest to slowest. This way time is not wasted on tests that do not apply to the string.
ie.
is the string longer than it should be - if it is return error code
is the string shorter than it should be - if it is return error code
go into more detailed tests
if you want it to be as efficient as possible we'd have to know more.
"I want to know if it has tree numbres, a space and four lettres. "
is a bit different than
"The text I have to compare is 10 caraters long and it have 3 possibilities"
what are the possibilities?
what would invalidate the string? having too many letters or too many numbers, the letters and numbers being in the wrong spots
be a bit more specific.
is the license plate thing the actual problem? or just an example you came up with
-
Exactly I am reading licence plates. I have to recognise those who are spanish. The spanish plates can be like that :
1111 AAA
or A(A) 1111 A(A) (the lettres whith () can appear or not)
or A(A) 11111
So I have to say if the plate read by the OCR is spanish or not.
Vart, whith your example, I have a little problem that is, if the enter is A 22 ret is 2 so I suppose that it is OK, but it isn't. Any idea?
-
Thank you both.
I do it with sscanf and checking the size of the arrays that it returns.
If you have an other solution, please tell me, because I am sure it will be a better one.
Thanks a lot.