Need help with string manipulation code

Hi, I just needed help understanding the bolded line, I don't understand how it inverts the characters, especially whats in the square brackets. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  //string manipulation; inverting order of characters in a c-string
#include <iostream>
using namespace std;
int main()
{
	char a[] = "This is a c-string!";
	char temp;
	string s = "Hello World!";
	strcpy(a,"Hello World!");
	for (int i = 0; i < strlen(a) / 2; i++)
	{
		temp = toupper(a[i]); a[i] = a[strlen(a) -i - 1]; a[strlen(a) -i - 1] = temp;
	}
	cout << a;
	//cout << strlen(a); 
	return 0;
}
Last edited on
Perhaps putting each statement on it's own line would help?
1
2
3
4
5
6
...
		temp = toupper(a[i]); 
		a[i] = a[strlen(a) -i - 1]; 
		a[strlen(a) -i - 1] = temp;
...


The first line converts character a[i] to an uppercase character. The next two lines are swapping the "last" character with the "first" character, then going to the next to last character and second character. After all the swapping is done the first half of the string will be lower case and the last half will be upper case.

Thanks a lot!
Topic archived. No new replies allowed.