字串的比較 strcmp 與 strncmp的使用與實作

  • 39571
  • 0
  • C++
  • 2019-04-23

strcmp是C語言的函式之一,來自C語言標準函式庫,定義於string.h,它需要兩個字串當作參數,比較兩個字串是否相等,相等就回傳 0,第一個字串大於第二個字串回傳正值,反之回傳負值。 

本文以C++實作執行。

strcmp與strncmp的函數原型如下:

int strcmp ( const char* s1, const char * s2 );

int strncmp ( const char* s1, const char * s2 , size_t n );

strcmp中,第一個參數s1和第二個參數s2指的是要互相比較字串長短的兩個參數,第一個字串大於第二個字串回傳正值,反之回傳負值,如果相等則回傳0。(比較字串時,是比較當下該個字元使用ASCII code相減)
原文定義: Compares thr string s1 with the string s2. The function retirns a value of zero, less than zero (usually -1) or greater than zero(usually 1) if s1 is equel to, less than or greater than s2, respectively.

strncmp中,第一個參數s1和第二個參數s2指的是要互相比較字串長短的兩個參數,比較的長度是第三個參數n,當第一個字串大於第二個字串回傳正值,反之回傳負值,如果相等則回傳0。(比較字串時,是比較當下該個字元使用ASCII code相減)
原文定義: Compares up to n characters of the string s1 with the string s2. The function returns zero, less than zero or greater than zero if the n-character portion of s1 is equal to, less than or greter than the corresponding n-character portion of s2, respectively.

strcmp與strncmp的實作程式碼如下: 

int strcmp_implementation(const char* s1, const char* s2)
{
	if (*s1 == '\0' && *s2 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	if (*s2 == '\0' && *s1 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	for (; *s1 != '\0' && *s2 != '\0'; s1++, s2++)//兩兩字串中的字元比較
	{
		if ((*s1 - *s2) > 0)
		{
			return (*s1 - *s2);
		}

		else if ((*s1 - *s2) <0)
		{
			return (*s1 - *s2);
		}
	}

	return 0;
}

int strncmp_implementation(const char* s1, const char* s2, int n)
{
	int count = 0;

	if (*s1 == '\0' && *s2 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	if (*s2 == '\0' && *s1 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	for (; *s1 != '\0' && *s2 != '\0' &&count<n; s1++, s2++,count++)//兩兩字串中的字元比較
	{
		if ((*s1 - *s2) > 0)
		{
			return (*s1 - *s2);
		}

		else if ((*s1 - *s2) <0)
		{
			return (*s1 - *s2);
		}
	}

	return 0;
}

strcmp與strncmp的完整程式碼如下: 

#include <iostream>
#include <iomanip>
using namespace std;


int strcmp_implementation(const char*, const char*);
int strncmp_implementation(const char*, const char*, int);

int main() {

	char x[20] = "";
	char y[20] = "h";
	char z[30] = "Hel";
	char *s1 = "Happy New Year";
	char *s2 = "Happy New Year";
	char *s3 = "Happy Hello";

	
	cout << "x:" << "NULL" << endl;
	cout << "y:" << y << endl;
	cout << "z" << z << endl;

	cout << "\nstrcmp_implementation(x,y):" << strcmp_implementation(x, y);
	cout << "\nstrcmp_implementation(y,z):" << strcmp_implementation(y, z);
	cout << "\nstrcmp_implementation(x,z):" << strcmp_implementation(x, z)<<endl;

	cout << endl;

	cout << "s1:" << s1 << endl;
	cout << "s2:" << s2 << endl;
	cout << "s3:" << s3 << endl;

	cout << "\nstrncmp_implementation(s1,s2,6):" << strncmp_implementation(s1, s2,6);
	cout << "\nstrncmp_implementation(s2,s3,5):" << strncmp_implementation(s2, s3,5);
	cout << "\nstrncmp_implementation(s1,s3,7):" << strncmp_implementation(s1, s3,7) << endl;

	system("pause");
	return 0;
}


int strcmp_implementation(const char*s1, const char*s2)
{
	if (*s1 == '\0' && *s2 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	if (*s2 == '\0' && *s1 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	for (; *s1 != '\0' && *s2 != '\0'; s1++, s2++)//兩兩字串中的字元比較
	{
		if ((*s1 - *s2) > 0)
		{
			return (*s1 - *s2);
		}

		else if ((*s1 - *s2) <0)
		{
			return (*s1 - *s2);
		}
	}

	return 0;
}

int strncmp_implementation(const char* s1, const char* s2, int n)
{
	int count = 0;

	if (*s1 == '\0' && *s2 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	if (*s2 == '\0' && *s1 != 0)//考慮其中一個字串為null的狀況
	{
		return (*s1 - *s2);
	}

	for (; *s1 != '\0' && *s2 != '\0' &&count<n; s1++, s2++,count++)//兩兩字串中的字元比較
	{
		if ((*s1 - *s2) > 0)
		{
			return (*s1 - *s2);
		}

		else if ((*s1 - *s2) <0)
		{
			return (*s1 - *s2);
		}
	}

	return 0;
}

實際執行結果如下:


有夢最美 築夢踏實

活在當下 認真過每一天

我是阿夢 也是Ace