Monday, July 19, 2010

Ignore Casing in C++

I finally wrote two functions that'll allow you to ignore casing in C++! While the coding is probably awful, I'll improve on these two later:

string toLowerCase(string mystr) {
  int reach = mystr.length();
  for (int i = 0; i < reach; i++) {
    if ((char)mystr[i] > (char)65 && (char)mystr[i] < (char)90) {
      mystr[i] = (char)mystr[i]+(char)32;
    }
  }
  return mystr;
}

string toUpperCase(string mystr) {
  int reach = mystr.length();
  for (int i = 0; i < reach; i++) {
    if ((char)mystr[i] > (char)96 && (char)mystr[i] < (char)122) {
      mystr[i] = (char)mystr[i]-(char)32;
    }
  }
  return mystr;
}

No comments:

Post a Comment