Coders Packet

Comparing Strings lexicographically using C++


Code to lexicographically compare two strings ignoring the case of letters. It is written in C++ language.

This is a project to compare two strings of the same length in lexicographic order.

Lexicographic order is based on the order of words in the dictionary. It is also known as dictionary order/lexical order. It is defined as ac as 'b' comes before 'c'.

This project compares two strings ignoring the cases of its letters, i.e. an uppercase letter is considered equivalent to the corresponding lowercase letter. For example, aaaA and AaAa are considered the same.

Inside the main function, two strings are declared and input is received from the user. Later On, the std::transform function is used to convert all the uppercase letters of these strings to lowercase. Further, running a for loop throughout the whole string compares the lexicographic order of each letter of both strings.

If the first string is less than second then -1 is printed, if the second is less than the first string then 1 is printed and if both the strings are the same then 0 will be the output.

Download Complete Code


No comments yet