Hi all.
I'm currently working my way through a beginners book on C++. In the book there is an exercise that demonstrates the difference between using array indexing and pointer arithmetic to access the elements of an array. The purpose of the program is to reverse the case of letters within a string.
There is a mistake in the book - instead of showing two versions of the code (one for array indexing and one for pointer arithmetic) the array indexing code is shown twice.
Can someone please write the code below using pointer arithmetic so i can see how it works?
// Reverse case using array indexing.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
int i;
char str[80] = "This Is A Test";
cout << "Original string: " << str << "\n";
for(i = 0; str[i]; i++) {
if(isupper(str[i]))
str[i] = tolower(str[i]);
else if(islower(str[i]))
str[i] = toupper(str[i]);
}
cout << "Inverted-case string: " << str;
return 0;
}
fyi - the book in question is 'C++ a beginner's guide by Herbert Schildt' - it is availble from
http://msdn.microsoft.com/en-us/beginner/cc305129.aspx
Chapter 4 (pages 30-31) is where the error is.