reversing array of char's issue

can someone plz tell me why the reverse function doesnt reverse the array?
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
35
36
37
38
39
40
#include <iostream>

using namespace std;

class my_string
{
	char* ptr;
	int size;
public:
	my_string(){};
	my_string(char* str) : ptr(str){};
	char* getstr (){return ptr;};
	void reverse();
	int find (char);
	void print();
};
	
void my_string::reverse()
{
     int counter=0;
     while (ptr[counter]!='\0')
	counter++;
	for (int i=0;i<(counter/2);i++)
	   {
	     char tmp=ptr[i];
	     ptr[i]==ptr[counter];
  	     ptr[counter]==tmp;
 	     counter--;
           }	
}

int main()
{

	my_string s1("abcde");
	s1.print();
    s1.reverse();
	s1.print();

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
void my_string::reverse()
{
     int counter=0;
     while (ptr[counter]!='\0')    //no {} for the while
	counter++;
	for (int i=0;i<(counter/2);i++)
	   {
	     char tmp=ptr[i];
	     ptr[i]==ptr[counter];
  	     ptr[counter]==tmp;
 	     counter--;
           }	
}

it should be:
1
2
3
4
5
6
7
8
9
10
11
while (ptr[counter]!='\0')    //no {} for the while
{
	counter++;
	for (int i=0;i<(counter/2);i++)
	   {
	     char tmp=ptr[i];
	     ptr[i]==ptr[counter];
  	     ptr[counter]==tmp;
 	     counter--;
           }
}
no.....still doesnt reverse the string
In the original code, counter doesn't change in the loop, so one of the chars you are working with is always the same char.

In JewelCpp's modified code, i is equal to counter, so you're assigning each char to itself.

The counter variable is unnecessary.
Doesn't reverse, as in "no change"?

Lines 26 and 27 are not assignments. == is a relational operator.

However, if you had that typo fixed, this would be your next problem:
1
2
3
4
5
while (ptr[counter]!='\0') counter++;
// assert( ptr[counter] == '\0' );
i=0;
ptr[i] = ptr[counter];
// assert( ptr[0] == '\0' ); 


Read the reference documentation for std::reverse for an another angle.


What is my_string::size intended for?
good question!

so i changed the code a bit thx to some clues here but still..doesnt work.
this is how it looks like now:

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
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

class my_string
{
	char* ptr;
	int size;
public:
	my_string(){};
	my_string(char* str) : ptr(str),size(strlen(ptr)){};
	char* getstr (){return ptr;};
	void reverse();
	int find (char);
	void print();
};
	
void my_string::reverse()
{
	 for (int i=0;i<(size/2);i++)
	   {
	     char tmp=ptr[i];
	     ptr[i]=ptr[size];
  	     ptr[size]=tmp;
 	     size--;
       }
}



while the main stay's the same
Q: What is wrong in this picture?
1
2
3
4
char * my_string::ptr;
my_string::my_string(char* str) : ptr(str) ...

my_string s1("abcde");

A: The "abcde" is a const string literal. Your "string" will point to something that should not be mutable.

Shouldn't the compiler make a remark about attempt to store an address of a string literal in non-const pointer?
Topic archived. No new replies allowed.