Review my code of pointer math!

Hello everyone,

1st time posting. This is not homework first of all. I am 38, hurt from a car wreck and haven't worked in several years, been in the hardware business for several years and I am learning c++ on my own to try to pick up some new job skills. With that out of the way I must say I am using a horrible book to learn c++ and the example code for using pointer arithmetic was left overlooked and the same code for pointer indexing was used twice. I think I was able to figure out the code but wanted it verified. I used a for loop and added the loop control value to index [0] to check each element for case. I believe a while loop could be better and will rewrite the code with that as well but I think the code I have now does the pointer arithmetic.

// reverse case using pointer arithmetic
#include <iostream>
#include <cctype>
using namespace std;

int main()

{

int i;
char *p;

char str[80] = "This Is A Test";
cout << "Original string: " << str << "\n";


p = str;

for(i = 0; p[i]; i++)
if(isupper(p[0 + i]))
p[0 + i] = (tolower(p[0 + i]));

else if(islower(p[0 + i]))
p[0 + i] = (toupper(p[0 + i]));







cout << "Inverted-case string: " << str << "\n";

return 0;

}

it compiles and runs correctly...I am just wondering if it is semi correct!

Thank you,

Dan
Yeah, but you aren't using any pointer arithmetic really.
You should use curly braces in your control statements. I know they aren't needed in this situation, but they would make the code much easier to read. Also, try to use fewer blank lines; that would also make it easier to read.

EDIT: Sorry, I meant curly braces, not parenthesis.
Last edited on
Topic archived. No new replies allowed.