Advertisement
cmiN

java vs c++

Mar 5th, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cctype>
  4.  
  5. char buffer[128], output[128];
  6.  
  7. int identify()
  8. {
  9.     bool java, cpp;
  10.     java = cpp = 0;
  11.     if (buffer[0] == '_' || buffer[strlen(buffer) - 1] == '_' || isupper(buffer[0])) {
  12.         return 3;
  13.     }
  14.     for (char* pch = buffer; *pch && !(java && cpp); pch++) {
  15.         if (*pch == '_') {
  16.             cpp = 1;
  17.         } else if (isupper(*pch)) {
  18.             java = 1;
  19.         }
  20.     }
  21.     if (!(java || cpp)) {
  22.         return 0;
  23.     } else if (java && cpp) {
  24.         return 3;
  25.     } else if (java) {
  26.         return 1;
  27.     } else if (cpp) {
  28.         return 2;
  29.     }
  30. }
  31.  
  32. void to_java()
  33. {
  34.     char* pch;
  35.     int i;
  36.     for (pch = buffer, i = 0; *pch; pch++, i++) {
  37.         if (*pch == '_') {
  38.             pch++;
  39.             output[i] = toupper(*pch);
  40.         } else {
  41.             output[i] = *pch;
  42.         }
  43.     }
  44.     output[i] = 0;
  45. }
  46.  
  47. void to_cpp()
  48. {
  49.     char* pch;
  50.     int i;
  51.     for (pch = buffer, i = 0; *pch; pch++, i++) {
  52.         if (isupper(*pch)) {
  53.             output[i++] = '_';
  54.             output[i] = tolower(*pch);
  55.         } else {
  56.             output[i] = *pch;
  57.         }
  58.     }
  59.     output[i] = 0;
  60. }
  61.  
  62. int main()
  63. {
  64.     int id;
  65.     while (scanf("%s", buffer) == EOF) {
  66.         id = identify();
  67.         if (id == 0) {
  68.             strcpy(output, buffer);
  69.         } else if (id == 1) {
  70.             to_cpp();
  71.         } else if (id == 2) {
  72.             to_java();
  73.         } else {
  74.             strcpy(output, "Error!");
  75.         }
  76.         printf("%s\n", output);
  77.     }
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement