Showing posts with label string sort. Show all posts
Showing posts with label string sort. Show all posts

String Comparison in C and C++ Languages and Working of strcmp function

In both C and C++ programming languages, an inbuilt function strcmp() is used to compare two strings. This function is defined in string.h header file. So, to use this function, you have to include the header file string.h in the program. Most of the people ( i mean beginners) have a wrong idea about the working of the this function. I also had made some false assumptions. This function takes two strings as arguments. And it compares the two strings using the ASCII equivalent of each character. Some of the false ideas about this function are:
  • The function returns 1 when the strings are equal and 0 when they are not equal
  • The function returns 1 when they are not equal
  • The function returns 1 when the first string is greater than the second and returns -1 when the first one is less than the second one