dynamic array allocation

Hello. Had an assignment using dynamic array allocation. Instructions: Write a program that prompts the user to input the length of a string as an integer, followed by the string and outputs the string in uppercase letters using dynamic arrays.

Not really sure how to use dynamic array allocaiton. Heres my code (sorry for bad english, from chile):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstring> 
#include <cctype> 
  
using namespace std;

int main()
{
    char str[81];
    char upperString[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++){
        char ch = str[i];
          if(ch!=" "){
              upperString[i] = toupper(ch);
          }
    }
        

    cout << upperString << endl;


    return 0;
}


if anyone can help I'd appriciate it
Have you considered searching the web for "dynamic array allocation" and seeing the links to tutorials that show up?

The basic setup is:

1
2
3
4
5
6
7
8
9
10
11
int length;
cin >> length; // input from user

char* str = new char[length+1]; // allocate 
str[length] = '\0'; // null terminate just in case (probably redundant but I don't feel like looking up documentation right now. I'm tired.)

cin.get(str, length); // get the actual string from user input

cout << str << '\n'; // further processing... e.g. display it

delete[] str; // clean up memory (don't leak memory!) 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstring>
#include <cctype> 
using namespace std;

int main()
{
   int length;
   cout << "Enter the length: ";   cin >> length;   cin.ignore( 1000, '\n' );
   char *str = new char[length+1];
   cout << "Enter string (up to " << length << " characters): ";
   cin.getline( str, length + 1 );
   for ( char *s = str; *s; s++ ) cout << (char)toupper( *s );
   delete [] str;
}
Last edited on
There's no requirement in the assignment to use separate memory for the upper case string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cctype>

int main() {
	size_t len {};

	std::cout << "Enter maximum length of string: ";
	std::cin >> len;
	std::cin.ignore();

	const auto str { new char[len + 1] };

	std::cout << "Enter a string: ";
	std::cin.getline(str, len + 1);

	std::cout << "\nString in upper case letters is:\n";

	for (auto st { str }; *st; ++st)
		*st = static_cast<char>(std::toupper(static_cast<unsigned char>(*st)));

	std::cout << str << '\n';

	delete [] str;
}


But in C++ it would be better to use managed pointer which removes the need for delete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <memory>
#include <cctype>

int main() {
	size_t len {};

	std::cout << "Enter maximum length of string: ";
	std::cin >> len;
	std::cin.ignore();

	const auto str { std::make_unique<char[]>(len + 1) };

	std::cout << "Enter a string: ";
	std::cin.getline(str.get(), len + 1);

	std::cout << "\nString in upper case letters is:\n";

	for (auto st { str.get()}; *st; ++st)
		*st = static_cast<char>(std::toupper(static_cast<unsigned char>(*st)));

	std::cout << str << '\n';
}

Last edited on
There is also a design decision about how to handle less consistent users. I could give these inputs:
3
Hello

or
42
answer

The programs above do cope with these, but is that the exact way you want them to do it?
Last edited on
Topic archived. No new replies allowed.