Function Name: caseInverted % Inputs (2): - (char) A first phrase % (char) A sec
ID: 3541745 • Letter: F
Question
Function Name: caseInverted
% Inputs (2): - (char) A first phrase
% (char) A second phrase
% Outputs (1): - (logical) A logical that represents if the second input is
% identical to the first, with an inverted case
%
% Function Description:
% Write a function called "caseInverted" that takes in 2 strings, and
% sees if they are the same exact phrases with opposite cases.
%
% For example, say we have 2 strings as follows:
%
% str1 = 'Bob Loves MATLAB.'
% str2 = 'bOB lOVES matlab.'
% Your answer should be true
%
% Notes:
% - You may not use upper(), lower(), or any other built in case function
% - Spaces and punctuation must match identically.
% - Logical indexing may be useful
%
% Test Cases:
% case1 = caseInverted('Bob Loves MATLAB.','bOB lOVES matlab.');
% case1 => true
%
% case2 = caseInverted('The function is called caseInverted.','tHE
% FUNCTION IS CALLED CASEiNVERTED.');
% case2 => true
%
% case3 = caseInverted('Bob Loves MATLAB.','bOBlOVESmatlab.');
% case3 => false
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
bool caseInverted(char* str1, char* str2)
{
int i = 0;
while(str1[i] != '' || str2[i] != '')
{
if(str1[i] <= 90 && str1[i] >= 65 && str2[i] - str1[i] == 32 ) {}
else if(str1[i] <= 122 && str1[i] >= 97 && str1[i] - str2[i] == 32 ) {}
else if(str1[i] < 65 || str1[i] >122 || (str1[i] < 97 && str1[i]>90))
{
if(str2[i] != str1[i])
return false;
}
else
return false;
i++;
}
if(str1[i] != str2[i])
return false;
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.