10.4: Library Functions for Working with C-Strings: 10714

Question

Write a function  max that has two C string parameters and returns the larger of the two.

Solution
char* max(char *a, char *b)
{
    int result = strcmp(a, b);

    if (result > 0)
        return a;
    else
        return b;
}

10.4: Library Functions for Working with C-Strings: 10713

Question

Given the  char * variables  name1 ,  name2 , and  name3 , write a fragment of code that assigns the largest value to the variable  max (assume all three have already been declared and have been assigned values).

Solution
max=name1;
if (strcmp(name2, max)>0)
    max=name2;

if (strcmp(name3,max)>0)
    max=name3;

10.3: C-Strings: 10900

Question

Assume that scotus is a two-dimensional character array that stores the family names (last names) of the nine justices on the Supreme Court of the United States. Assume no name is longer than twenty characters. Assume that scotus has been initialized in alphabetical order. Write an expression whose value is the last name stored in the array.

Solution
scotus[8]

10.3: C-Strings: 11122

Question

Declare a char array named  line suitable for storing C-strings as large as 50 characters, and write a statement that reads in the next line of standard input into this array. (Assume no line of input is 50 or more characters.)

Solution
char line[50];
cin.getline(line,50);