Write a function named void firstLast(char theString[], char *first, char *last) { } It should accept a character string, and set the char pointed to by first to the character of the string which is earliest in the alphabet. It should set the charaxcter pointed to by last to be the element of the string appearing last in the alphabet.

Respuesta :

Answer:

C++

#include <iostream>

using namespace std;

//////////////////////////////////////////////////////////////////////////////

void firstLast(char theString[], int array_length, char* first, char* last) {

   first = &theString[0];

   last = &theString[0];

   //////////////////////////////////////////

   for (int i=1; i<array_length; i++) {

       if ((int)*first > (int)theString[i]) {

           first = &theString[i];

       }

       

       if ((int)*last < (int)theString[i]) {

           last = &theString[i];

       }

   }

   //////////////////////////////////////////

   cout<<"first-> "<<*first<<endl;

   cout<<"last-> "<<*last<<endl;

}

//////////////////////////////////////////////////////////////////////////////

int main() {

   char theString[] = {'c', 'a', 't', 's', 'd', 'A'};

   char* first, *last;

   int array_length = (sizeof(theString)/sizeof(*theString));

   //////////////////////////////////////////////////////////

   firstLast(theString, array_length, first, last);

   //////////////////////////////////////////////////////////

   return 0;

}

Explanation:

I have used ASCII code values to compare alphabets and figure out whether a char is before or after another char. ASCII code values of chars can be found, in C++, by casting them with (int).

ACCESS MORE