The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Help in strcmp
Discuss Help in strcmp in the C Programming forum on Dev Shed. Help in strcmp C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 4th, 2013, 09:39 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
|
|
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){
}

|

January 4th, 2013, 10:47 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

January 4th, 2013, 11:58 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation 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 ....
|

January 5th, 2013, 12:27 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

January 5th, 2013, 02:20 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation 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
|

January 5th, 2013, 02:55 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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?
|

January 5th, 2013, 03:26 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation 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 ......
|

January 5th, 2013, 04:14 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

January 5th, 2013, 06:37 AM
|
 |
Contributed User
|
|
|
|
|

January 5th, 2013, 07:18 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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: Quote: | "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.
|

January 7th, 2013, 03:09 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
|
|
Quote: | 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 ..
Quote: | 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 ...
|

January 7th, 2013, 04:44 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | 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!
|

January 8th, 2013, 07:56 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation 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 ... 
|

January 8th, 2013, 08:00 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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.
|

January 9th, 2013, 12:52 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 56
Time spent in forums: 15 h 12 m 50 sec
Reputation Power: 0
|
|
Quote: | 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 ....
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|