C語言 · 比較字串

來源:互聯網
上載者:User

標籤:bin   實現   字串   inf   input   編程   1.0   res   put   

演算法訓練 比較字串  時間限制:1.0s   記憶體限制:512.0MB      編程實現兩個字串s1和s2的字典序比較。(保證每一個字串不是另一個的首碼,且長度在100以內)。若s1和s2相等,輸出0;若它們不相等,則指出其第一個不同字元的ASCII碼的差值:如果s1>s2,則差值為正;如果s1<s2,則差值為負。範例輸入java basic範例輸出8 
 1 #include<stdio.h> 2 #include<string.h> 3 int main(){ 4     char s1[100],s2[100]; 5     scanf("%s%s",&s1,&s2); 6     int len1 = strlen(s1); 7     int len2 = strlen(s2); 8     if(len1 == len2){ 9         for(int i=0;i<len1;i++){10             if(s1[i] == s2[i]){11                 printf("0");12                 return 0;13             }14         }15     }else if(len1<len2){16         for(int i=0;i<len1;i++){17             if(s1[i] != s2[i]){18                 printf("%d",s1[i]-s2[i]);19                 return 0;20             }21         }22     }else if(len1>len2){23         for(int i=0;i<len2;i++){24             if(s1[i] != s2[i]){25                 printf("%d",s1[i]-s2[i]);26                 return 0;27             }28         }29     }30 }

 

C語言 · 比較字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.