Thread: Help in strcmp

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0

    Exclamation Help in strcmp


    i tried to use strcmp in comparing a data but im not satisfied for the result it is posible to use strcmp to compare multiple data ? can you please help me to solve my problem ... please

    here is my code in strcmp
    char data1[10],data[10];
    data1[0] = NULL;
    if (strcmp(data1, data2) != 0){
    }

    :tntworth: :tntworth: :tntworth: :confused:
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Each call to strcmp compares two strings. That is what it does. What else are you wanting it to do?

    Your sample code does not make much sense:
    Code:
        char data1[10],data[10];
        data1[0] = NULL; 
        if (strcmp(data1, data2) != 0)
        {
        }
    You don't use data and you don't declare data2, so I guess that the declaration was a typo.

    NULL is a pointer, not a char. The char, according to Campbell's book on serial communication, would be NUL, with one "L", but to my knowledge NUL means nothing in a C program. Rather, what you want to use is the char literal, '\0'.

    If you need to review the difference between a char and a pointer, then ask.

    And you still need to explain just what it is that you're wanting to do.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    And you still need to explain just what it is that you're wanting to do.[/QUOTE]

    actually this is the data source and i want to compare according to fruit, veggies, and drinks, etc.

    the data source is not in order like this ...

    001 fruit apple
    002 fruit mango
    003 vegetable carrots
    004 vegetable radish
    005 drinks water
    006 fruit water melon
    999 fruit melon


    please help me ....
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Compare what to what?

    Each comparison between two strings is a separate call to strcmp. If you need to make multiple comparisons, that will call for multiple calls to strcmp.

    Your problem is not with strcmp itself, but rather with the overall program and algorithm (how you are going to make all those comparisons and then make them give you the results you want). What we are lacking is that overall algorithm.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    i want to compare the set of description like fruit, vegetables, and drinks and i will form it like this

    fruit // main head
    001 apple
    002 mango
    006 water melon
    999 melon // etc

    vegetables // main head
    003 carrots
    004 radish
    //etc

    drinks // main head
    005 water
    501 softdrinks //etc

    the number before the apple and etc will be the sequence of data ...

    this will be the output it is posible to use strcmp in multiple data the source will get in a flatfile ... thanks
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Huh?

    In my first civilian job, I was ahead of everybody else because I was the only one with a degree in computer science and I had two years experience with Pascal going in (at that time, aerospace was gearing up for Ada, for which there was not yet any certified compiler, so everybody was using Pascal which was a stepping stone to Ada; one book was divided into two parts: Part One, how incredibly like Pascal Ada is, and Part Two, how incredibly unlike Pascal Ada is.

    In my second civilian job, I was completely behind everybody else. Every week, we had to map our progress according to our planned progress. The problem I had was that we "code pigs" naturally think in terms of writing code. However, the secret to software development is to do most of your work in designing the software, after which the coding phase is very short and uneventful. A "code pig" immediately starts to write code, even though he has no idea what that code is supposed to do. Obviously, the "code pig's" approach wastes a lot of time and effort.

    Screw the actual code! What is your design? Before you write even one single line of code, what exactly is it that you are trying to do? And how exactly do you intend to do it? Once you are able to state that, then anybody would be able to code it.

    Most of the work in software development is in the design! The rest could be done by a trained monkey!

    So then, what is your design?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    sir i dont encounter the pascal programming even C language
    i dont have any idea about this language but
    i want to learn from it so i want to ask and help me
    if the "strcmp" is posible to use to compare multiple data like what i ask in this thread ... and how can i do

    sorry for misunderstand ......
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    WTF????

    Design!!!!!!!!!!

    Just what the **** are you trying to do????????????

    Decide just what exactly it is that you want to do and how to do it!!!!!!!!

    Once you have decided that, then coding it is ridiculously simple.

    So just what exactly are you trying to do? Your message #5 makes absolutely no sense to me. Until you can precisely define just what exactly you want to do, there is nobody on this entire planet who could possibly produce the code that you need. But once you precisely define just what exactly you want, then the implementation will be trivial.

    So it is entirely up to you to precisely define what comparisons you want to be performed.

    We are still waiting.
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,396
    Rep Power
    1871
    Seems like a lost cause to me.
    http://forums.devshed.com/c-programm...ng-936992.html
    OP persistently goes out of their way to make it really hard to offer any help at all.

    I'm far from convinced that they learnt a damn thing in the last 6 months as the first thread is pretty much the same as this thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    In C a string is an array of characters with a '\0' character indicating the end of the useful data. The strcmp() function compare one string with another.

    In your example, the strings being compared were not initialised to anything remotely useful. You would do better to copy and paste your actual code since your example does nothing to classify your intent.

    Given your data list, you will need to write code that reads that data and tokenises it into individual strings that can them be compared.

    It is entirely unclear what you mean by "compare multiple data". As I said strcmp() compares exactly one string with exactly one other. It is not a sort function.

    If you need to process data of any kind (especially in a proprietary format) there is unlikely to be a single existing standard function that will magically do it for you, you do actually have to program it! The standard library is a set if building blocks not a set of canned solutions.

    The point is there is a huge gap between what you are asking (very low level operation) and what you appear to be attempting to do (very high level operation). Perhaps what you should do is ask a question about what you want to achieve. Such as for example:
    "Given the data below in a file data.dat, how could I generate a file sorted_data.dat with the data sorted numerically using the first space delimited field as the sort order key?"
    You could ask a question like that on whatever it is you want to do, and it would be clear and unambiguous, however this is not a free programming service, it is a forum for those interested in programming and learning, so you would need to show some effort to start with - some credible attempt at a solution. You might then get help with specific areas of difficulty or confusion; you are unlikely to get a complete solution.

    The answer as it happens would involve file handling, string handling, and sorting - all of which are supported by the standard library, but you are perhaps being too specific by asking only about strcmp() and expecting it to be capable of solving your entire problem. Programming does not work like that!
    Last edited by clifford; January 7th, 2013 at 04:34 AM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    Originally Posted by clifford
    As I said strcmp() compares exactly one string with exactly one other. It is not a sort function.
    thank sir this could be the answer that i want to know ....
    my program that i create is for transaction and reports and compare any transaction according to details heres my code sir
    http://forums.devshed.com/c-program...ing-936992.html
    by the way thanks for the help sir ... sorry if i dont understand the other deep words in proframming ..

    Originally Posted by salem
    http://forums.devshed.com/c-program...ing-936992.html
    yes sir its same problem coming from me .. thats all my coding the others are usefull to input ...
  22. #12
  23. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    Originally Posted by lhon12006
    sorry if i dont understand the other deep words in proframming
    Most of what I said had little to do with programming, but rather how to ask a coherent question. You have not heeded that advice and done nothing to clarify your question. If there is code related to this question, post it in this question directly rather than linking to a different question. The link you gave has been elided so cannot be followed. If that question is in fact the same question, stop creating new threads (unless perhaps you can manage to post a coherent one).

    I provided no information about strcmp() that is not already in widely available documentation for that function so it is difficult to see how you could have misunderstood its purpose!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    thank you sir
    can you suggest
    what is the best use in comparing multiple data or to substitute in strcmp in my program ....

    thanks again sir ... :trockon:
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Originally Posted by lhon12006
    thank you sir
    can you suggest
    what is the best use in comparing multiple data or to substitute in strcmp in my program ....

    thanks again sir ... :trockon:
    Yet again: one pair at a time.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    Originally Posted by dwise1_aol
    Yet again: one pair at a time.
    that could be the case sir in strcmp so it cannot recognize the unsorted data ....
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo