Holey_yan

20140603_KUAS_VisualC++_Exam13

Jun 3rd, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. /************************************/
  2. /** 20140603 Exam 13               **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/06/03 am.11:40   **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h> //字元測試函數庫
  11.  
  12. //做轉換大小寫、字串中數字相乘之功能function
  13. void function(char *inputPtr);
  14.  
  15. void main()
  16. {
  17.     printf("Enter a string: ");
  18.     char input[50] = {EOF};
  19.     scanf("%[^\n]", input);
  20.  
  21.     function(input); //input 前不需加取址符號(&), 因陣列本身就代表一連串的指標
  22. }
  23.  
  24. void function(char *inputPtr)
  25. {
  26.     char output[50] = {EOF}; //Answer of string
  27.     int number_multiply = 1; //Answer of number to multiply
  28.  
  29.     int counter_string = 0; //counter of string
  30.     while(1)
  31.     {
  32.         //當字串讀取到結尾處時
  33.         if(*inputPtr == '\0')
  34.         {
  35.             break;
  36.         }
  37.         else if(*inputPtr != '\0')
  38.         {
  39.             //islower = 判斷字元是否為小寫
  40.             if(islower(*inputPtr))
  41.             {
  42.                 //toupper = 將字元由小寫轉換為大寫
  43.                 output[counter_string] = toupper(*inputPtr);
  44.             }
  45.             //isupper = 判斷字元是否為大寫
  46.             else if(isupper(*inputPtr))
  47.             {
  48.                 //tolower = 將字元由大寫轉為小寫
  49.                 output[counter_string] = tolower(*inputPtr);
  50.             }
  51.             else
  52.             {
  53.                 //其它狀況照常輸出, 如空格、數字等
  54.                 output[counter_string] = *inputPtr;
  55.             }
  56.             //add answer counter
  57.             counter_string++;
  58.  
  59.             //isdigit = 判斷此字元是否為數字(0 - 9)
  60.             if(isdigit(*inputPtr))
  61.             {
  62.                 number_multiply *= (int)*inputPtr - 48;
  63.             }
  64.  
  65.             //移至下個指標
  66.             *inputPtr++;
  67.         }
  68.     }
  69.  
  70.     printf("Answer of string is: %s.\n", output);
  71.     printf("Answer of number to multiply: %d.\n", number_multiply);
  72.     system("pause");
  73. }
Advertisement
Add Comment
Please, Sign In to add comment