c - Changing upper to lower and lower to upper -
i new in c programming. ask if function ok, because don't know how check in main
.
char uppertolowertoupper(char ch) { if (ch>='a' && ch<='z') { ch=tolower(ch); return ch; } else if (ch>='a' && ch<='a') { ch=toupper(ch); return ch; }
it may appear work you, improved.
- firstly, it's incomplete. you're missing bracket (and
return statement
) @ end. - next, not c implementations use ascii. if use code on systems ebcdic character set, you'd find
ch>='a' && ch<='z'
includes more alphabet characters, , similarch>='a' && ch<='z'
... suggest usingisupper
,islower
instead. - finally,
tolower
,toupper
expect arguments of typeint
.char
arguments convertedint
there's more that... arguments supposed eitherunsigned char
values oreof
. else undefined behaviour, meaning signed character values pass these functions might cause segfaults (e.g. this question). recommend changing function acceptsunsigned char
argument (instead ofchar
argument) , returnsunsigned char
.
Comments
Post a Comment