使用C比较两个字符串是否相等

一、概述

  案例:使用C语言编写一个方法比较两个字符串是否相等如果相等则返回0,否则返回不等于0的数字。

二、代码示例

#include <iostream>

using namespace std;

/**
 * 比较两个字符串是否相等
 * */
int mystrcmp(char *str1,char * str2){
	int i =0;
	while(str1[i]==str2[i]){
		if(str1[i]==''){
			return 0;
		}
		i++;

	}
	return str1[i]>str2[i]?1:-1;
}

// int mystrcmp(char *str1,char *str2){
// 	while(*str1==*str2){
// 		if(*str1==''){
// 			return 0;
// 		}
// 		str1++;
// 		str2++;

// 	}
// 	return *str1 > *str2?1:-1;

// }

int main(int argc, char const *argv[])
{
	char *str1 = "hello world";
	char *str2 = "hello";
	int ret = mystrcmp(str1,str2);
	if(ret==0){
		cout << "equals"<<endl;
	}else{
		cout << "not equals"<<endl;
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/tony-yang-flutter/p/15392443.html