【c++範例】char字串比較程式

by - 清晨5:31


#include <iostream>
using namespace std;
int Mystrcmp(char *,char *);
int main(void)
{
  int result;
  char word1[ ] = "I like C";    //字串1
  char word2[ ] = "This is fun";  //字串2
  result = Mystrcmp(word1,word2);
  if(!result)
    cout<<"word1 equal word2\n";
  else
    cout<<"word1 does not equal word2\n";
  return 0;
}
int Mystrcmp(char *str1,char *str2)
{
  int i;
  for(i=0;!(*(str1+i) == '\0' && *(str2+i) == '\0'); i++)
    if(*(str1+i) != *(str2+i)) return -1;
  return 0;
}

你可能會喜歡

0 意見