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, , similar ch>='a' && ch<='z'... suggest using isupper , islower instead.
  • finally, tolower , toupper expect arguments of type int. char arguments converted int there's more that... arguments supposed either unsigned char values or eof. else undefined behaviour, meaning signed character values pass these functions might cause segfaults (e.g. this question). recommend changing function accepts unsigned char argument (instead of char argument) , returns unsigned char.

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -