Advertisement
paulogp

Upper case

Jul 13th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. /* Conversão de letras minúsculas em letras maiúsculas */
  2. // Apple Xcode
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h> // toupper
  8.  
  9. #define NCAR 80
  10.  
  11. void func_uppercase(char *s)
  12. {
  13.     // from lower case to upper case
  14.     // return via pointer
  15.     for (int i = 0; i < strlen(s); i++)
  16.     {
  17.         if (s[i] >= 'a' && s[i] <= 'z')
  18.         {
  19.             s[i] = (char)((int)s[i] + (int)'A' - (int)'a');
  20.         }
  21.     }
  22. }
  23.  
  24. void func_uppercase2(char *s)
  25. {
  26.     // lower case to upper case
  27.     // return via pointer
  28.     for (int i = 0; i < strlen(s); i++)
  29.     {
  30.         s[i] = toupper(s[i]);
  31.     }
  32. }
  33.  
  34. int main (int argc, const char * argv[])
  35. {
  36.     // upper case
  37.     char s[NCAR];
  38.  
  39.     // input string
  40.     printf("string: ");
  41.     fgets(s, NCAR, stdin);
  42.  
  43.     // converting "manually"
  44.     func_uppercase(s);
  45.     printf("manualmente: %s\n", s);
  46.  
  47.     // using toupper
  48.     func_uppercase2(s);
  49.     printf("usando toupper: %s\n", s);
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement