You MUST use point era and dynamic allocation

Question

You MUST use point era and dynamic allocation to complete the following exercise to receive points write a program that takes a positive integer as input, indicating the number of characters (separated by white spaces) that will follow_

(1) Create a dynamic char array with the size equal to the value of the input positive integer to store the user input characters If the input inter is not positive, the program MI output ‘Invalid size” and exit.

(2) Turn al the lower case alphabetic letters into upper case letters and output the updated char array Ex if the user inputs (the individual characters are separated by white spaces)
10 h a p p y ! I M !
J
the program would output (no whitespaces In the output)
HAPPY! I’ M!

(3) Output the last alphabetic letter in the updated array in the following format
The last letter is <the last alphabetic letter in the array

DC given the above result, the program will output

The last letter in M

You MUST define and call a changeToUppero function to change all lower case letters in the array to their upper case counterparts` This function takes a char pointer pointing to the dynamic array and its size as input. void changeToUpper(char *array, int nCha rs) ; You MUST define and call a findLastLerter0 function to return the memory of the last element that contains an alphabetic letter. This function takes a char pointer pointing to the dynamic array and its size as input.

char *tindLastLetter( char *array, int nChars); Mint: The functions toupper(char), isupper(char) and islower(char) will be useful in this exercise. No additional libraries required. To search the last alphabetic letter, start from the last element of the array and go backward. NOTE THAT: you will receive 20 (mints deduction if you do not Meese the dynamic array before the end Of the program to avoid memory
leaks.
:W44,74,11,1NV:t1 cxlmir
:L’ .1h 1.’ilt!,’•ti:i-J4,;.444c;ii,

Summary

Here in the dynamic allocation given question, we have to define and call a changeToUpper function to change all lower case letters in the array to their upper case counterparts. The user will enter the string and the last letter will get printed on the screen as output.

Explanation

First, we add the header files. Then we add the function name as changeToUpper so the string will get converted into the upper case. Next, we declare a variable with string datatype to store the elements in the array with no white spaces. After that, we remove space and digits from the string. So after that, we print the string. And after that in the main code, we have our output statement and also we have to call our functions.

Code

#include <iostream>
using namespace std;
#include <ctype.h>
#include <cstring>
/* to change a string to upper case */
void changeToUpper(char *array, int &nChars)
{
    /* to store the character array without spaces */
    string s="";
    for(int i=0;i<nChars;i++)
    {
        /* removing spaces and digits */
        if(array[i]==' ' || (array[i]>='0'&&array[i]<='9')){}
        else
        {
            /* converting the letters to uppercase */
            if((array[i]>='A' && array[i]<='Z')||(array[i]>='a'&&array[i]<='z'))
            s=s+char(toupper(array[i]));
            else 
            s=s+array[i];
        }
    }
    /* to have the string in upper case */
    for(int i=0;i<s.length();i++){
        array[i]=s[i];
        cout<<array[i];
    }
    cout<<endl;
    /* updating the new array length */
    nChars=s.length();
}
/* to find the last letter in the character array */
char findLastLetter(char *array, int nChars)
{
    for(int i=nChars;i>=0;i--){
        if(array[i]>='A' && array[i]<='Z')
        return array[i];
    }
}
/* Main code */
int main()
{
    int n;
    cout<<"Enter number of character: ";
    cin>>n;
    char *array=new char[n];
    cout<<"Enter the string: \n";
    cin.ignore();
    cin.getline(array,n);
    changeToUpper(array,n);
    cout<<"The last letter is "<<findLastLetter(array,n)<<endl;
}

Output

use point era and dynamic allocation

 

Also read, Z Three Numbers

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *