text comparing

This is a discussion on text comparing within the C Programming forums, part of the General Programming Boards category; Hello, I am a begginer in C programming, an I have a problem I can't fix. The problem is that ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    22

    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.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    use isdigit and isalpha functions
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    look at sscanf

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    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.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    sscanf has nothing to do with keyboard - it scans text buffers
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    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.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    > 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.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    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.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    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.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    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.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    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

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    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?
    Last edited by Picachu; 12-12-2006 at 08:52 AM.

  15. #15
    Registered User
    Join Date
    Dec 2006
    Posts
    22
    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.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21