Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program Pointers and Classe Write two versions of a string-comparison functi

ID: 646015 • Letter: C

Question

C++ Program Pointers and Classe

Write two versions of a string-comparison function. The functions take a third argument specifying the number of characters in each string to compare. These functions should act like function 'strncmp'. The first version should use array subscripting, and the second should use pointers and pointer artithmetic.

Please use the template below and write lines of code for where it asks for it. (In the /* */ comment sections)

C++ Program Pointers and Classe Write two versions of a string-comparison function. The functions take a third argument specifying the number of characters in each string to compare. These functions should act like function 'strncmp'. The first version should use array subscripting, and the second should use pointers and pointer artithmetic. Please use the template below and write lines of code for where it asks for it. (In the /* */ comment sections)

Explanation / Answer

#include <iostream>

using namespace std;

int stringCompare1(char s1[], char s2[]);
int stringCompare2(char s1[], char s2[]);

int main(){
   char string1[100], string2[100];
   cout << "Enter two strings: ";
   cin >> string1 >> string2;
  
   cout << "The value returned from stringCompare1( "" << string1 << "", "" << string2 << "") is "
   << stringCompare1(string1, string2) <<
   " The value returned from stringCompare2( "" <<
   string1 << "", "" << string2 << "") is " << stringCompare2(string1, string2) << endl;
  
   return 0;
}

int stringCompare1(char s1[], char s2[]){
   int index;
   for(index = 0; ; ++index){
       if(s1[index] == '' && s2[index] == ''){
           return 0;
       }
       else if(s1[index] < s2[index]){
           return -1;
       }
       else if(s1[index] > s2[index]){
           return 1;
       }
   }
}

int stringCompare2(char s1[], char s2[]){
   for(; ; s1 += 1, s2 += 1){
       if(*s1 == '' && *s2 == ''){
           return 0;
       }
       else if(*s1 < *s2){
           return -1;
       }
       else if(*s1 > *s2){
           return 1;
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote