Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. static const int MAXLEN = 11;
  5.  
  6. // rather than side-effect of changing input string,
  7. // we use a local variable. so the returned string is changed.
  8. // so don't bother storing return values without copying them elsewhere.
  9. char *cStringtoUpper(const char *lower)
  10. {
  11.     static char upper[MAXLEN+2];
  12.     int i = 0;
  13.  
  14.     while (lower[i] != 0)
  15.     {
  16.         if (lower[i] >= 'a' && lower[i] <= 'z')
  17.             upper[i++] = lower[i] - 0x20;
  18.         else
  19.             upper[i++] = lower[i];
  20.     }
  21.  
  22.     // dont forget to null terminate the new string!
  23.     upper[i] = 0;
  24.  
  25.     return upper;
  26. }
  27.  
  28. int main()
  29. {
  30.     int i;
  31.     char line[MAXLEN+2];
  32.  
  33.     cout << "This program will convert strings up to " << MAXLEN <<
  34.         " characters of length into uppercase.\n" <<
  35.         "You may exit by inputting 'Q' (in uppercase!)\n\n";
  36.  
  37.     while (1)
  38.     {
  39.         cout << "Enter string: ";
  40.  
  41.         cin.get(line, MAXLEN+1);
  42.  
  43.         cin.ignore();   // strip the '\n'
  44.  
  45.         // easy check for quit. make sure it's Q alone!
  46.         if (line[0] == 'Q' && line[1] == 0)
  47.             break;
  48.  
  49.         cout << "In uppercase: " << cStringtoUpper(line) << endl;
  50.     }
  51.  
  52.     cout << "\nGoodbye!\n\n";
  53.  
  54.     // signals success to executing environment.
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement