answersLogoWhite

0

#include<iostream>

#include<string>

int compare (const std::string& a, const std::string& b)

{

int n=1;

std::string::const_iterator ai=a.begin(), bi=b.begin();

for (int n=1; ai!=a.end() && bi!=b.end(); ++ai, ++bi, ++n)

{

char ca = *ai;

char cb = *bi;

if (ca<cb) return n * (-1);

if (cb<ca) return n;

}

if (ai==a.end() && bi==b.end()) return 0;

if (ai==a.end()) return n * (-1);

return n;

}

int main()

{

std::cout

<< "When comparing strings, zero indicates the two strings are equal.\n"

<< "A negative value indicates the first string is less than the second string.\n"

<< "A positive value indicates the first string is greater than the second string.\n"

<< "The absolute value indicates the characters that differ in the strings. Thus\n"

<< "-10 or 10 indicate that the 10th characters show a difference.\n"

<< std::endl;

std::string x = "This is a string.";

std::string y = "This is another string.";

std::string z = "This is another.";

std::cout << "String x = "" << x << """ << std::endl;

std::cout << "String y = "" << y << """ << std::endl;

std::cout << "String z = "" << z << """ << std::endl;

std::cout << std::endl;

std::cout << "compare(x,x) = " << compare(x,x) << std::endl;

std::cout << "compare(x,y) = " << compare(x,y) << std::endl;

std::cout << "compare(x,z) = " << compare(x,z) << std::endl;

std::cout << "compare(y,x) = " << compare(y,x) << std::endl;

std::cout << "compare(y,y) = " << compare(y,y) << std::endl;

std::cout << "compare(y,z) = " << compare(y,z) << std::endl;

std::cout << "compare(z,x) = " << compare(z,x) << std::endl;

std::cout << "compare(z,y) = " << compare(z,y) << std::endl;

std::cout << "compare(z,z) = " << compare(z,z) << std::endl;

std::cout << std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

Make programme for multification of three no in c plus plus?

result = a * b * c;


What are the steps to execute a c plus plus programme?

Build it, link it, run it.


Does C allows you to use equality operators to compare strings?

Yes, it does.


How will you compare turbo c to c plus plus?

"http://wiki.answers.com/Q/How_will_you_compare_turbo_c_to_c_plus_plus_language"


Do you have to use delete operator on C plus plus strings before exiting your application?

no you dont


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


What is the use of strcmp function in c plus plus?

strcmp is used to compare two strings. If the return value is zero, the two strings are the same. If the return value is less than 0, then the first string is less than the second string, otherwise the first string is greater than the second string. Strings are compared lexicographically, character by character.


How will you compare turbo c to c plus plus language?

"http://wiki.answers.com/Q/How_will_you_compare_turbo_c_to_c_plus_plus_language"


Can a user compile c programme using c plus plus compiler?

Usually, but not always. For example the following is legal in C, but illegal in C++: char new [3] = "ABC";


How can you get a specific string from large string using c plus plus strings?

std::string::substr();


How do you write a programme to print a plus bi in c plus plus?

#include &lt;iostream&gt; int main() { std::cout &lt;&lt; "a plus bi" &lt;&lt; std::endl; return 0; }


How do you use this function in c programme?

I don't use that function in C programme.