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, 2022 in C++ by Nicholas
• 7,760 points
566 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 Jun 20, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
757 views
0 votes
0 answers

How to use setprecision in C++

I'm new to C++ and all I want to do is print my point number up to two digits.  For example, if the number is 3.444, the output should be 3.44, and if the number is 99999.4234, the output should be 99999.42.  How am I going to do that?  The value changes over time.  This is my code. #include <iomanip.h> #include <iomanip> int main() { ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
762 views
0 votes
0 answers

How to use enums in C++

Suppose we have an enum like the following: enum Days ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
528 views
0 votes
0 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

As a mathematician, one of my pet peeves of C-derived languages is : (-1) % 8 // comes out as ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
418 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,020 points
686 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,020 points
2,386 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
1,448 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,740 points
1,854 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, 2022 in C++ by Damon
• 4,960 points
1,137 views
0 votes
1 answer

How to use enums in C++

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

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
616 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP