what are the differences of string and cstring header files and how does the pre defined function of strcmp which is called string comparison works???? i know it gives 0 if string 1 = string 2 gives +ive and -ive if str1 is greater or less but it calculates that which one is greater????someone help??
The two headers are completely different. cstring is inherited from C and provides functions for working with C-style strings (arrays of char terminated by '\0'). string was born in C++ and defines the std::string class along with its non-member functions.
sorry elaborate how it works in the background.....does it compares the ascii of the strings one by one or compares the sum of ascii of all or strlen or what????? how it returns +ive and -ive value....
The strcmp() function compares the ASCII values of the characters one by one. If all the characters match, it returns zero; otherwise, if the first differing character in the first string has a lower ASCII encoding than the compared character in the second string, it returns a negative, wheras if the first string's character is higher, it returns positive.
Mind you, it's important to keep the difference between C-strings and string objects in mind, as they are quite different things. The fact is that C-strings are something of a second-class type in C, which carries over to C++; they are as much a convention as they are an actual type, because it is up to the functions which operate on them to do things like check the size of the string, ensure that there is a zero-delimiter at the end of the string, and so forth. The C++ string class and objects are quite different, being fully qualified objects in the language with a large amount of functionality built into them. They are more opaque than C-strings, but more flexible, and for most purposes, easier to work with. While it is farily easy to convert C-strings to strings (using the string c'tor) and back (using the .c_str() method), they are not at all the same thing.