(Comparing Strings) Write a program that uses function strncmp to compare two strings input by the user. The program should input the number of characters to compare. The program should state whether the first string is less than, equal to or greater than the second string.
What will be an ideal response?
```
#include
#include
using namespace std;
const int SIZE = 20;
int main()
{
char string1[ SIZE ];
char string2[ SIZE ];
int result;
int compareCount; // number of characters to compare
cout << "Enter two strings: ";
cin >> string1 >> string2;
cout << "How many characters should be compared: ";
cin >> compareCount;
// call function strncmp to compare the two strings
result = strncmp( string1, string2, compareCount );
if ( result > 0 )
cout << '\"' << string1 << "\" is greater than \"" << string2
<< "\" up to " << compareCount << " characters\n";
else if ( result == 0 )
cout << '\"' << string1 << "\" is equal to \"" << string2
<< "\" up to " << compareCount << " characters\n";
else
cout << '\"' << string1 << "\" is less than \"" << string2
<< "\" up to " << compareCount << " characters\n";
cout << endl;
return 0; // indicates successful termination
} // end main
```
Enter two strings: sand sandpaper
How many characters should be compared: 4
"sand" is equal to "sandpaper" up to 4 characters
You might also like to view...
In Excel, when creating subtotals, you should create the subtotals before you sort the data
Indicate whether the statement is true or false
A(n) ________ error is a software or hardware problem that prevents a program from working correctly during execution.
Fill in the blank(s) with the appropriate word(s).
?
A. Addresses violations harmful to society and is actively enforced and prosecuted by the state. B. Regulates the structure and administration of government agencies and their relationships with citizens, employees, and other governments. C. Defines socially acceptable behaviors. D. One of the first attempts to protect federal computer systems by establishing minimum acceptable security practices. E. A collection of statutes that regulates the interception of wire, electronic, and oral communications. F. Focuses on enhancing the security of the critical infrastructure in the United States. G. The study of what makes actions right or wrong, also known as moral theory. H. An approach that applies moral codes to actions drawn from realistic situations.
What are the primary input object types? Provide a code example of two different input types.
What will be an ideal response?