Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.)/*Your code from Chapter 8, exercise 5 is below.Rewrite the following code to using dynamic arrays.*/#include #include #include using namespace std;int main(){char str[81];int len;int i;cout << "Enter a string: ";cin.get(str, 80);cout << endl;cout << "String in upper case letters is:" << endl;len = strlen(str);for (i = 0; i < len; i++)cout << static_cast(toupper(str[i]));cout << endl;return 0;}

Respuesta :

Answer:

Following are the code to this question:

#include <iostream>//defining header file

#include <cstring>//defining header file

#include <cctype>//defining header file

using namespace std;

int main()//defining main method

{

char *str = new char[80];//defining char variable for input value

char *str1=new char[80]; //defining char variable for storing the output of string

int len,i;//defining integer variable

cout << "Enter a string: ";//print message

cin.get(str, 80);//use get method to input string value

cout << "String in upper case letters is:"<< endl;//print message

len = strlen(str);//using len variable to store string length

for (i = 0; i <len; i++)//defining for loop for generating sting into upper case

{

  *(str1+i)=toupper(*(str+i)); //generating upper case string of input string.

}

for(i=0;i<len;i++)//defining for loop for print value

{

  cout<<*(str1+i);    //displaying upper case string value.

}

return 0;

}

Output:

Enter a string: database

String in upper case letters is:

DATABASE

Explanation:

In the given code two char array "str and str1", and two integer variable "i and len", in the next step, the str array is used the get method to store array value and it defines the two for loop, in which the first string is used len variable to count the length of the string length and in the second for loop, it converts the string value into upper case and prints its value.