-
Changing character case
I am writing this function on changing character strings to either all uppercase or all lowercase. I am supposed to return wether or not a change was made. I am to recieve a string to change and number flag (myChangeType) to tell whether to change to capitals (eToUpper) or lowercase (eToLower)
* Use the flag (myChangeType) to record whether or not any changes
* were made.
* myChangeType=eFalse means no change was made.
* myChangeType=eTrue means a change was made.
heres what i have so far... i dont know how to use the flag ...any help would be appreciated
Code:
int myStrChangeCase(unsigned char first_string [], myChangeType name)
{
int i;
int myChangeType;
/* Lowercase to Uppercase */
for(i=0; first_string[i] !='\0'; i++);
{
if( first_string[i] >= 'a' && first_string[i] <='z')
first_string[i]= first_string[i] - (32);
}
/* Uppercase to Lowercase */
for(i=0; first_string[i] !='\0'; i++);
{
if( first_string[i] >= 'A' && first_string[i] <='Z')
first_string[i]= first_string[i] + (32);
}
return myChangeType;
}
-
You should be using the functions isupper, islower, toupper and tolower. Not all character sets have a through z the same. Also, it's not set in the standard that the difference between A and a is 32. There is no such specification.
Also, what's the point in returning the change type, since you're passing it to the function? I suspect you want something like:
Code:
if( changeType == LowerMe )
{
...lowering code...
}
else
{
...raising code...
}
Otherwise the way you have it, everything gets converted through both cases loops.
Quzah.
-