C++ help please and thank you with some homework! can you also prove it works th
ID: 646024 • Letter: C
Question
C++ help please and thank you with some homework! can you also prove it works thank you
#include <iostream>
#include <string>
using namespace std;
// Compiling this program generates a number of compiler errors.
// After compiling the program, run it to confirm that it behaves according
// to the following description.
// This program is intended to accept two strings from the terminal
// and check to see if one is a case-reversed version of the other.
// That is, if corresponding characters are the opposite case.
// The following commands indicate the desired operation. The Makefile
// has these examples built in.
//
// $ ./a.out abcDEF XYZmno
// abcDEF and XYZmno have opposite case.
// $ ./a.out abCDEF XYZmno
// abCDEF XYZmno have the same case in position 2.
// $ ./a.out abCDE XYZmno.
// abCDE XYZmno have different lengths
//
int main()
{
string str1(argv[1]);
string str2(argv[2]);
size_t length = str1.size();
if (length != str2.size())
cout << str1 << " and " << str2 << " have different lengths." << endl;
else
{
// If we are here, the strings have the same length
int i = 0;
while i < length
{
char ch1 = str1[i];
char ch2 = str2[i];
if ((isUpper(ch1) && isUpper(ch2)) ||
(isLower(ch1) && isLower(ch2)))
break;
i++;
}
if (i < length)
cout << str1 << " and " << str2 <<
" have the same case in position " << i << "." << endl;
else
cout << str1 << " and " << str2 <<
" have opposite case." << endl;
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include<ctype>
using namespace std;
int main(int argc, char *argv[])
{
string str1(argv[1]);
string str2(argv[2]);
size_t length = str1.size();
if (length != str2.size())
cout << str1 << " and " << str2 << " have different lengths." << endl;
else
{
// If we are here, the strings have the same length
int i = 0;
while (i < length)
{
char ch1 = str1[i];
char ch2 = str2[i];
if ((isUpper(ch1) && isUpper(ch2)) ||
(isLower(ch1) && isLower(ch2)))
break;
i++;
}
if (i < length)
cout << str1 << " and " << str2 <<
" have the same case in position " << i << "." << endl;
else
cout << str1 << " and " << str2 <<
" have opposite case." << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.