Reversing char[] arrays

I tried making a program to reverse a given char[] array.
The problem: If you insert a big char array, let say, with 15 chars, a runtime error happens.

Second problem: When you type in "abcde", for example, the output will be "edcba²²²²". I can't see what is causing this.

Source code:

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
#include <iostream>
using namespace std;

char* reverse(char*);

int main() {
	
	char* text;
	text = new char[];

	cout << "Input text to reverse";
	cin >> text;

	cout << reverse(text);
}

char* reverse(char* text) {

	for (int i=0;text[i]!='\0';i++) {
		if (text[i+1]=='\0') {
			char *newText;
			newText = new char[i-1];
			for (int n=0,j=i;n<=i,j>=0,newText[n]!='\0';n++,j--) {
				newText[n]=text[j];
			}
					return newText;
		}
	}
}


Thanks in advance
1
2
char* text;
	text = new char[];

You must specify in brackets for how many elements you allocate memory. It must be at least length of input string +1

And what's the tricky method for reversing a string you use??
You don't need to specify how much elements I want to allocate since I'm using dynamic memory, by the use of the keyword new. The name dynamic says it all.

And for the method. Imagine it this way:

You type in abcde.

Array text:

[0] [1] [2] [3] [4] --> int n starts with the value of 0, which is the element A.
A B C D E

Array newText:

[0] [1] [2] [3] [4] --> int j starts with the value of i, since it represents the elements, it is 4.
E D C B A

then you do it.
n=0, j=4;
n=1,j=3;
n=2,j=2;
n=3,j=1;
n=4,j=0;

And using the assignment operator it appropriately assigns the values.

But still, my problem wasn't solved
E D C B A
Last edited on
You don't need to specify how much elements I want to allocate since I'm using dynamic memory, by the use of the keyword new. The name dynamic says it all.


Kibestar, you're crazy man! Very funny explanation of dymanic memory allocation :)
Runtime error you complain about is most probably due to what i said.
The original code doesn't even compile for me. It's not valid C++! How are you getting a run-time error? What compiler do you use?
You don't need to specify how much elements I want to allocate since I'm using dynamic memory, by the use of the keyword new. The name dynamic says it all.


You must have a compiler that is reading your mind then. How exactly does the compiler know how to figure out the amount of memory to allocate?
There are different ways of skinning a cat.....
You could use the reverse() function from the Standard C++ Generic Algorithms
e.g.
1
2
3
4
5
6
7
8
9
10
   char s[] = "abcde fghij";
    cout<<"print string s \n";
    cout<<s;
    reverse(s,s+11);
    cout<<"\nprint reversed string s \n";
    cout<<s;
cout<<"Press any key to continue...";
cin.get();
return 0;    
}  

/*print string s
abcde fghij
print reversed string s
jihgf edcba
Press any key to continue...*/
Kibestar, you're crazy man! Very funny explanation of dymanic memory allocation :)
Runtime error you complain about is most probably due to what i said.


Why the hell would it be any different from normal static arrays if I had to say precisely how much elements I want to alocate? I don't want something fixed. It's D-Y-N-A-M-I-C.

dy·nam·ic (dī-nām'ĭk)
adj. also dy·nam·i·cal (-ĭ-kəl):

1. Of or relating to variation of intensity, as in musical sound.

2. Characterized by continuous change, activity, or progress

http://dictionary.reference.com/browse/dynamic



The original code doesn't even compile for me. It's not valid C++! How are you getting a run-time error? What compiler do you use?


I use Windows XP Home Edition SP3 2000. Compiler: Microsoft Visual C++ Express Edition 2008

It's not valid C++!


You must be using a toaster to compile this code.


You must have a compiler that is reading your mind then. How exactly does the compiler know how to figure out the amount of memory to allocate?


The compiler knows by how much chars you input! DYNAMIC, IT CHANGES!!!!! If I put 1 char, it is text[1], if I put 2, such as "ab" it will be text[2]. IT VARIES!!!!!
Last edited on
The compiler doesn't insert itself into your program. It isn't editing the code while the program is running.
Why the hell would it be any different from normal static arrays if I had to say precisely how much elements I want to alocate? I don't want something fixed. It's D-Y-N-A-M-I-C.
It's dynamic in the sense that its size can be given at run time, not in the sense that its size can change. All C and C++ arrays are of fixed size.

It's true. It's not valid C++. And no, the compiler can't know at compile time what your input will be at run time.

If you're going to ask questions, you should be ready to accept the answers you're given.
Last edited on
Topic archived. No new replies allowed.