[Date Prev][Date Next][Date Index]
[intv] convert string to upper or lower case
//Problem: convert string to upper case or lowercase.
//Save as: main.cpp and compile as: g++ -o main main.cpp
#include <iostream>
#include <string.h> //required for strlen
using namespace std;
char * toUpperOrLower(char *, int);
int main(){
char *str1 = "A Toyota! Race fast, safe car. A Toyota.";
cout << str1 << " ** " << toUpperOrLower(str1, 1) << endl;
cout << str1 << " ** " << toUpperOrLower(str1, 0) << endl;
}
char * toUpperOrLower(char *a, int UpOrLow){
char *c;
c = (char *)malloc(sizeof(char)*strlen(a));
if (NULL == c) {cout << "ERROR" << endl;}
for (int i=0; i < strlen(a); i++){
c[i] = a[i];
if (UpOrLow == 0){
//lower case
if ((a[i] >= 0x41) && (a[i] <= 0x5a)){
c[i] = (char)(a[i] | 0x20);
}
}else{
//upper case
if ((a[i] >= 0x61) && (a[i] <= 0x7a)){
c[i] = (char)(a[i] & 0xdf);
}
}
}
return c;
}
Comments and corrections are appreciated and can be sent to
papers@mia.ece.uic.edu.
Click here for ©opyright information.
|