homework help - csting reverse characters using pointers.

Can someone tell me how to at least start correcting my program?? I submitted to instructor for review and his response was:
Your reverse routine is correct.
The only problem is that you reversed the copy (reverse) of cstring, not cstring itself.

Please reverse the characters in the cstring.

Make result point to the first character of cstring
Make swapper point to the last character of cstring

Then you use while statement as it is EXCEPT the while condition
In this case, the correct while condition should be (result<=swapper)
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>
using namespace std;

#include <cstring>

int main( )
{
	char cstring[10]; 
	int t = 0; //used to assign temporary variable

	cout << "Please enter a character string : ";
	cin >> cstring;

	char *reverse = new char[strlen(cstring) + 1];
	char *swapper = cstring + strlen(cstring);
	char *result = reverse;
	while(swapper != cstring)
	{
		swapper--;
		*result = *swapper;
		result++;
	}

	*result = '\0';
	cout << "CString = : " << reverse;
	cout << endl;
    system("PAUSE");
	return 0;
}
As your instructor rightly pointed out,
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>
using namespace std;

#include <cstring>

int main( )
{
	char cstring[10]; 
	int t = 0; //used to assign temporary variable

	cout << "Please enter a character string : ";
	cin >> cstring;

	char *reverse = new char[strlen(cstring) + 1];
	char *swapper = cstring; // not + strlen(cstring);
	char *result = reverse;
	while(result <= swapper) // please check carefully whether it should be <= or <.
	{
		swapper--;
		*result = *swapper;
		result++;
	}

	*result = '\0';
	cout << "CString = : " << reverse;
	cout << endl;
    system("PAUSE");
	return 0;
}
Thank you - this does not solve my problem, it returns a blank. Any other ideas??
Sorry my bad, try this:
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
#include <iostream>
#include <cstring>
using namespace std;

int main( )
{
	char* cstring[10]; 

	cout << "Please enter a character string : ";
	cin >> cstring;

	char *swapper = cstring + strlen( cstring );
	char *result = cstring;
// Please see the below post by vlad
/*	while(result <= swapper) // please check carefully whether it should be <= or <.
	{
		swapper--;
		*result = *swapper;
		result++;
	}*/


	*result = '\0';
	cout << "CString = : " << cstring;
	cout << endl;
    system("PAUSE");
	return 0;
}
Last edited on
You should not allocate a new string. As the instructor said you should reverse the string in place.

1
2
3
4
5
6
7
8
9
10
11
char *result = cstring;
char *swapper = cstring + strlen( cstring );

while ( result + 1 < swapper )
{
   char c = *--swapper;
   *swapper = *result;
   *result++ = c;
}

cout << "CString = : " << cstring << endl;
Last edited on

@abhishekm71

while(result <= swapper) // please check carefully whether it should be <= or <.
{
swapper--;
*result = *swapper;
result++;
}



Your code is wrong because you simply overwrite characters pointed by result by characters pointed by swapper. You shoud apply swapping not overwritting.

@vlad
yes sorry I missed that, thanks for correcting
Thanks so much, that works, but I only get the first three characters in reverse order for the cout??? I know I am doing something wrong, just don't know how to fix it...
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>
#include <cstring>
using namespace std;

int main( )
{
	char cstring[10]; 
	int t = 0; //used to assign temporary variable


	cout << "Please enter a character string : ";
	cin >> cstring;

	char *result = cstring;
    char *swapper = cstring + strlen( cstring );

while(result <= swapper) // please check carefully whether it should be <= or <.
{
swapper--;
*result = *swapper;
result++;
}

	*result = '\0';
	cout << "CString = : " << cstring;
	cout << endl;
    system("PAUSE");
	return 0;
}
The instructor says I need to use strlen(cstring)-1 and prefix & ??
Why did not you read what was written by me above?
I did, what do you mean?
Please read vlad's post here and you will understand what you and I were doing wrong.

http://www.cplusplus.com/forum/beginner/107869/#msg586107
Thank you for explaining, I still don't really understand but after adjusting my code, it works. Have nice day!
Topic archived. No new replies allowed.