Wednesday, March 20, 2013

Program to compare two strings

After reading about string. Now we are comparing two strings. It is very easy to compare two numbers using some comparison operators (>,<,>=,<=,==,!=). But if we are talking about strings, these operators are not generally applicable to them. If we want to manipulate a string then we can use some inbuilt functions of string.h header file.
Here we are using strcmp() function to compare two strings. This function takes two strings as argument for their comparison.If both strings are equal then it returns 0. If first string is less then second string then it will return any negative value and in the last case, when first string is greater then second string it returns a positive value.
This function compares characters of both strings according ASCII (American Standard Code for Information Interchange) values, then gives result.

This simple program will clear how comparison will be done-

Powered by Blogger.