How to use new and delete operator in C

0 votes

I'm presently studying C++ and have completed the following task in which I must transform an array from uppercase to lowercase and vice versa:

int main()
{
    char str[100];
    cout << "Enter anything: ";
    cin.getline(str, 100);

    //upper to lower vice versa
    for (int i = 0; i < 100; i++) {
        if (str[i] == 0x20)
        {
            continue;
        }
        str[i] ^= 0x20;
    }
    cout << "output: " << str;

return 0;

}

However, they want me to use the new[] and delete[] operators instead of declaring the numbers in an array, which is what this portion char str[100]; does. I've attempted to use it, but the notion confuses me. Is there anything you might suggest? Any assistance will be much appreciated! Thank you so much in advance!

Jun 15 in C++ by Nicholas
• 2,060 points
10 views

1 answer to this question.

0 votes
int main(){
    char *str;
    int sz = 0;

    std::cout << "enter number of characters: ";
    std::cin >> sz;

    str = new char[sz + 1];

    std::cout << "Enter anything: ";
    std::cin.ignore();
    std::cin.getline(str, sz + 1);

    //upper to lower vice versa
    for (int i = 0; i < sz; i++) {
        if (str[i] == 0x20)
        {
            continue;
        }
        str[i] ^= 0x20;
    }
    std::cout << "output: " << str;

    delete[] str;
    return 0;
}

You allocate memory on the heap with new, and you deallocate memory with delete[].

Because the getline appends the 0 character at the end, you must set the array size to sz + 1.

answered 6 days ago by Damon
• 3,380 points

Related Questions In C++

0 votes
0 answers

How to traverse stack in C++?

Is traversing std::stack possible in C++? It is not possible to traverse using the following method.  Because there is no member end in std::stack. std::stack<int> foo; // .. for (__typeof(foo.begin()) it = foo.begin(); ...READ MORE

Jun 1 in C++ by Nicholas
• 2,060 points
24 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2 in C++ by Nicholas
• 2,060 points
20 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered 5 days ago in C++ by Damon
• 3,380 points
14 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 3,380 points
15 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
190 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,366 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
785 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,720 points
581 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 3,380 points
15 views
0 votes
1 answer

How to use enums in C++

This will be sufficient to declare your ...READ MORE

answered 6 days ago in C++ by Damon
• 3,380 points
8 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP