Mega Code Archive

 
Categories / C++ / Pointer
 

Index a pointer as if it were an array

#include <iostream>  #include <cctype>  using namespace std;    int main()  {    char *p;    int i;    char str[80] = "This Is A Test";      cout << "Original string: " << str << endl;      p = str;                           // assign p the address of the start of the array         for(i = 0; p[i]; i++) {            // index p      if(isupper(p[i]))        p[i] = tolower(p[i]);      else if(islower(p[i]))        p[i] = toupper(p[i]);    }      cout << "Inverted-case string: " << str;      return 0;  }