strcmp函數

strcmp函數用於c語言中兩個字符串比較(只能夠比較字符串,不能夠比較數字)

規則

當s1>s2時,返回爲正數;函數

當s1=s2時,返回值爲0;spa

當s1<s2時,返回爲負數;字符串

兩個字符串自左向右相比,比較ASCLL值大小,到'/0'中止。

  例如:‘a’<'b'  'A'<'B'  "computer">"compare"get

實例

#include<stdio.h>
#include<string.h>  //頭文件
main()
{
  char s1[20],s2[20];
  int i;
  gets(s1);
  gets(s2);
  i=strcmp(s1,s2);  //strcmp比較結果賦值給i
  if(i>0)      
    printf("%s",s1);
  else if(i==0)
    printf("s1=s2");
  else
    printf("%s",s2);
}string

輸入:

  abcd  efg;

輸出結果:

  efg;
相關文章
相關標籤/搜索