字串數字從小到大輸出

來源:互聯網
上載者:User

華為面試時,面試官出的一道題,將一個隨機的整數轉換成一個按各位上數值大小排序的整數,例如整數2541轉換成1245,隨機整數521368轉換成123568,用C語言編程來實現,要求不能使用一步到位的庫函數.

[cpp]
view plaincopy
  1. #include <stdio.h>  
  2. void func(char *str)  
  3. {  
  4.     if(NULL == str)  
  5.         return ;  
  6.     unsigned int s[10] = {0};  
  7.     const char *p = str;  
  8.     while('\0' != *p)  
  9.     {  
  10.         ++ s[*p++ - '0'];  
  11.     }  
  12.     unsigned int i ,j, nCount = 0;  
  13.     for(i = 0; i < 10; ++ i)  
  14.     {  
  15.         for(j = 0; j < s[i]; ++ j)  
  16.         {  
  17.             str[ nCount++ ] = i + '0';  
  18.         }         
  19.     }  
  20.     str[nCount] = '\0';   
  21. }  
  22. void main()  
  23. {  
  24.     //無論多大的資料都當作字串處理  
  25.     char str[] = "999888877773333111100001423412412341234123412412342";  
  26.     func(str);  
  27.     printf("%s\n",str);  
  28. }  

有一段類似這樣的字串:"7C7C307C724C7C267C7C7C7C7C3A7C417C7C7" 其中每兩位兩位的代表一個字元,類似:“7C”對應“|”、“24”對應“$”、“41”對應“A”、“26”對應“&”等等。。。想用純C寫這樣一個函數,

[cpp]
view plaincopy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <math.h>  
  4.   
  5. char FromStrToChar(char *str)  
  6. {  
  7.     if(NULL == str || strlen(str) != 2)  
  8.         return '\0';  
  9.   
  10.     //對應十六進位資料  
  11.     int a[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};  
  12.   
  13.     unsigned int i,sum = 0;  
  14.   
  15.     for(i = 0; i < 2; ++ i)  
  16.     {  
  17.         if(str[i] >= '0' && str[i] <= '9')  
  18.         {  
  19.             sum += a[str[i] - '0'] * (16/pow(16,i));  
  20.         }  
  21.         else  
  22.         {  
  23.             sum += a[str[i] - 'A' + 10] * (16/pow(16,i));  
  24.         }  
  25.     }  
  26.           
  27.     return (char)sum;  
  28.   
  29. }  
  30.   
  31. void StringConvert(char *str)  
  32. {  
  33.     unsigned int len = strlen(str);  
  34.     if(len & 1)  
  35.     {  
  36.         //字串數目為奇數,編碼有問題,自動清空  
  37.         //你可以根據自己情況處理  
  38.         *str = '\0';  
  39.         return ;  
  40.     }  
  41.   
  42.     unsigned int i, nCount = 0;  
  43.     char a[3] = {0};  
  44.   
  45.     for(i = 0; i < len / 2; ++ i)  
  46.     {          
  47.         a[0] = str[i*2];//第一位  
  48.         a[1] = str[i*2 + 1];//第二位  
  49.         a[2] = '\0';  
  50.   
  51.         str[nCount ++] = FromStrToChar(a);  
  52.     }  
  53.     str[nCount] = '\0';  
  54. }  
  55.   
  56. void main()  
  57. {  
  58.     char str[] = "7C7C307C724C7C267C7C7C7C7C3A7C417C7C";  
  59.   
  60.     StringConvert(str);  
  61.   
  62.     printf("%s\n",str);  
  63.   
  64. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.