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

1- Write an expression that evaluates to true if the value ofthe string variable

ID: 3618571 • Letter: 1

Question

1- Write an expression that evaluates to true if the value ofthe string variable s1 is greater than the value of stringvariable s2 .
2-  Write an expression that evaluates to true ifthe value of variable lastName is greater than the string Dexter .
3-  Write an expression that evaluates to true ifand only if the string variable s does not equal end.
4- Given the string variables name1 and name2 ,write a fragment of code that assigns the larger of the two to thevariable first (assume all three are already declared and name1 and name2 assigned values).
5- Given the char * variables name1 , name2 , and name3 , write a fragment of code thatassigns the largest value to the variable max (assume allthree have already been declared and have been assignedvalues).
6- Write a function max that has two stringparameters and returns the larger of the two, that is, a stringshould be returned. (By larger we mean alphabetically, thus thestring "b" is greater then the string "apple". The lengths of thetwo strings are not being compared.)
7- Write a function min that has three stringparameters and returns the smallest.
("Smallest" in the alphabetical sense: "apple" is smaller than"zoo", even though "zoo" is SHORTER than "apple"-- because "apple"comes before "zoo" in the dictionary.)
2-  Write an expression that evaluates to true ifthe value of variable lastName is greater than the string Dexter .
3-  Write an expression that evaluates to true ifand only if the string variable s does not equal end.
4- Given the string variables name1 and name2 ,write a fragment of code that assigns the larger of the two to thevariable first (assume all three are already declared and name1 and name2 assigned values).
5- Given the char * variables name1 , name2 , and name3 , write a fragment of code thatassigns the largest value to the variable max (assume allthree have already been declared and have been assignedvalues).
6- Write a function max that has two stringparameters and returns the larger of the two, that is, a stringshould be returned. (By larger we mean alphabetically, thus thestring "b" is greater then the string "apple". The lengths of thetwo strings are not being compared.)
7- Write a function min that has three stringparameters and returns the smallest.
("Smallest" in the alphabetical sense: "apple" is smaller than"zoo", even though "zoo" is SHORTER than "apple"-- because "apple"comes before "zoo" in the dictionary.)

Explanation / Answer

1)

Syntax for strcmp:

int strcmp(const char *str1, const char *str2)

Therefore, the required expression is strcmp(str1,str2)>0

2)

The required expression is strcmp(lastName,”Dexter”)>0

3)

Therefore, the required expression is strcmp(s,”end”)!=0

4)

    

//check which string is greater

if(strcmp(name1,name2)>0)

{

//assign name1 to first, if the

     //name1 is greater than name2

     first=name1;

}

else

{

     //assign name2 to first, if the

     //name2 is greater than name1

     first=name2;

}

5)

//compare name1 and name2

     if(strcmp(name1,name2)>0)

     {

          //compare name1 and name3

          if(strcmp(name1,name3)>0)

          {

              //assign name1 to max, becuase

              //name1 is greater than name2 and name3

              max=name1;

          }

          else

          {

              //assign name3 to max, because

              //name3 is greater than name1 and name2

              max=name3;

          }

     }

    else

     {

          //compare name2 and name3

          if(strcmp(name2,name3)>0)

          {

              //assign name2 to max, because

              //name1 is greater than name1 and name3

              max=name2;

          }

          else

          {

              //assign name3 to max, because

              //name3 is greater than name1 and name2

              max=name3;

          }

     }

6)

//returns the larger of the two strings

char* max(char * s1,char *s2)

{

          //compare s1 and s2

          if(strcmp(s1,s2)>0)

          {

              //return s1, becuase s1 is greater than s2

              return s1;

          }

          else

          {

              //return s1, becuase s1 is greater than s2

              return s2;

          }

}

7)

//returns the smaller of the three strings

char* min(char * s1,char *s2,char *s3)

{

          //compare s1 and s2

     if(strcmp(s1,s2)>0)

     {

          //compare s2 and s3

          if(strcmp(s2,s3)>0)

          {

              //return s3, because s3 is smaller than s1 and s2

              return s3;

          }

          else

          {

              //return s2, because s2 is smaller than s1 and s3

              return s2;

          }

     }

    else

     {

          //compare name2 and name3

          if(strcmp(s1,s3)>0)

          {

              //return s3, because s3 is smaller than s1 and s2

              return s3;

          }

          else

          {

              //return s1, because s1 is smaller than s2 and s3

              return s1;

          }

     }

}