#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;
}
Yes, it does.
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.
std::string::substr();
I don't use that function in C programme.
c strings are terminated by \0 character
result = a * b * c;
Build it, link it, run it.
Yes, it does.
"http://wiki.answers.com/Q/How_will_you_compare_turbo_c_to_c_plus_plus_language"
no you dont
No.
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.
"http://wiki.answers.com/Q/How_will_you_compare_turbo_c_to_c_plus_plus_language"
Usually, but not always. For example the following is legal in C, but illegal in C++: char new [3] = "ABC";
std::string::substr();
#include <iostream> int main() { std::cout << "a plus bi" << std::endl; return 0; }
I don't use that function in C programme.