[YTU]_2424 C语言习题 字符串比较
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2424 C语言习题 字符串比较
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
寫一函數,實現兩個字符串的比較。即自己寫一個strcmp函數,函數原型為
int stringcmp(char *p1,char *p2);
設p1指向字符串s1,p2指向字符串s2。要求當s1=s2時,返回值為0,若s1≠s2,返回它們二者第1個不同字符的ASCII碼差值(如"BOY"與"BAD",第2個字母不同,"O"與"A"之差為79-65=14)。如果s1>s2,則輸出正值,如s1<s2,則輸出負值。
Input
兩個字符串
Output
比較結果
Sample Input
BOY BADSample Output
14#include <iostream> using namespace std; int stringcmp(char*p11,char*p22) {for(;*p11!='\0',*p22!='\0';p11++,p22++){if(*p11!=*p22)return *p11-*p22;}if(*p11=='\0'&&*p22=='\0')return 0;}int main() {int stringcmp(char *,char *);int m;char str1[80],str2[80],*p1,*p2;cin>>str1;cin>>str2;p1=&str1[0];p2=&str2[0];m=stringcmp(p1,p2);cout<<m<<endl;return 0; }總結
以上是生活随笔為你收集整理的[YTU]_2424 C语言习题 字符串比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2417 C语言习题 字符串
- 下一篇: [YTU]_2716 统计不及格人数