1.11M Members

what is the difference between string and cstring header files in C++

 
0
 

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??

 
0
 

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.

strcmp works like this:

int strcmp(const char *a, const char *b)
{
    while (*a == *b) {
        if (*a == '\0') {
            return 0;
        }

        ++a;
        ++b;
    }

    return *a < *b ? -1 : +1;
}
 
0
 

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....

 
0
 

It compares each character in turn. If they have the same numeric value, it moves to the next character. When a mismatch is found, it returns the difference of the two numeric values[1].

Positive and negative are easy to see when looking at the values as being numeric:

1 - 2 = negative
2 - 1 = positive
1 - 1 = 0

[1] That's all that's required by an implementation. Some implementations, like mine above, will enforce strict -1, 0, or +1 as the return value.

 
0
 

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.

 
1
 

The strcmp() function compares the ASCII values of the characters one by one

It compares the numeric values of the characters. It's not safe to assume that the character set is ASCII. Granted, ASCII and Unicode (where ASCII is a subset) are pervasive, but but not universal.

 
0
 

Schol-R-LEA thanks alot...it was a great help for such elaborate breif....it has really cleared my doubts...thanks decetikon as well

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: