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

(Exercise) Create - array2.cpp Write a program that takes a string input by the

ID: 3910346 • Letter: #

Question

(Exercise) Create - array2.cpp Write a program that takes a string input by the user, then swaps the cases of all alphabets and reverses the string. To do this, your program should ask the user to input a string, by displaying "Enter the string to reverse and have cases swapped for each character: "on the console. Read the string input by the user, create a string that contains the characters in the reverse order with the cases swapped, and then output "The reverse of the string with cases swapped is: ", followed by the new string. For example: If the input string is "IAb2" the new string and output is "2BaL" If the input string is "HomeWork" the new string and output is "KROwEMOh" · If the input string is "l', the new string and output is “1” Remember that in C++, a string is simply an array of characters. Consequently, array indexing can be used to get desired characters in the string

Explanation / Answer

Please find the required program:

//===================================================

#include <iostream>
#include<cstring>
using namespace std;

int main() {
char str[100],temp;
int i=0,j;
std::cout << "Please enter the desired string to be modified: " << std::endl;
std::cin >> str;

// Swapping the case of the alphabets present in the string
while(str[i]!='')
{
if(isupper(str[i]))
{str[i]=tolower(str[i]);}
else if(islower(str[i]))
{str[i]=toupper(str[i]);}
i++;
}
// i now contains the length of the string

// Reversing the String
for(j=0;j<i/2;j++)
{
    temp=str[j];
    str[j]=str[i-j-1];
    str[i-j-1]=temp;
}
std::cout << "The reversed and case swapped string is: "<<str<< std::endl;
return 0;
}

//=================================================