Advertisement
jzh4n

Gaya Penulisan Variabel

Jul 12th, 2020
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     string s;
  9.  
  10.     cin >> s;
  11.  
  12.     bool is_snake = false;
  13.     int len = s.length();
  14.  
  15.     for(int i = 0; i < len; ++i) {
  16.         if(s.at(i) == '_') {
  17.             is_snake = true;
  18.  
  19.             break;
  20.         }
  21.     }
  22.  
  23.     string output = "";
  24.  
  25.     if(is_snake) {
  26.         for(int i = 0; i < len; ++i) {
  27.             if(s.at(i) == '_') {
  28.                 i = i + 1;
  29.  
  30.                 if(i < len) output += toupper(s.at(i));
  31.             } else output += s.at(i);
  32.         }
  33.     } else {
  34.         for(int i =  0; i < len; ++i) {
  35.             if(isupper(s.at(i))) {
  36.                 output += '_';
  37.                 output += tolower(s.at(i));
  38.             } else output += s.at(i);
  39.         }
  40.     }
  41.  
  42.     cout << output << '\n';
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement