字串的切割 strtok 與字串的長度 strlen的使用與實作

strtok是C語言的函式之一來自C語言標準函式庫,定義於string.h,它需要兩個字串參數,以第二個參數字串當中的內容來切割第一個參數字串。

strlen是C語言的函式之一來自C語言標準函式庫,定義於string.h,它用來回傳字串的長度。

本文以C++實作執行。

strtok和strlen的函數原型如下:

char *strtok(char* s, const char* delim);

size_t strlen(const char *s);

strtok中,第一個參數s代表的是要被切割的字串,第二個參數delim是你想要什麼內容來切割第一個字串。
原文定義:A sequence of call to strtok breaks string s into "token"-logical pieces such as words in a line of text. The string is broken up based on the characters contained is string delim . For instance, if we were to break the string "this:is:a:string" into token based on tha character ':', the resulting token would be "this", "is", "a" and "string". Function strtok returns only one token at a tome, however. The first call contains s1 as the first argument, and subsequent calls to continue tokenzing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned.

strlen中,參數s代表要回傳的字串長度內容。
原文定義: Determines the length of string s. The number of characters preceding the terminating null character is returned.

strtok與strlen的實作程式碼如下:

char *strtok_implementation(char *s, const char *delim)
{
	char *spanp;
	int c, sc;
	char *tok;
	static char *last;


	if (s == NULL && (s = last) == NULL)
	{
		return NULL;
	}

	c = *s++;
	for (spanp = (char*)delim; (sc = *spanp++); ) {
		if (c == sc) {
			c = *s++;
			spanp = (char*)delim;
		}
	}

	if (c == 0) {		
		last = NULL;
		return (NULL);
	}
	tok = s - 1;


	while(1) {
		c = *s++;
		spanp = (char *)delim;
		do {
			if ((sc = *spanp++) == c) 
			{
				if (c == 0)
				{
					s = NULL;
				}
				else
				{
					s[-1] = 0;
				}
				last = s;
				return (tok);
			}
		} while (sc != 0);
	}
}

int strlen_implemention(const char *s)
{
	int count = 0;
	for (;*s!='\0';count++,s++)
	{

	}

	return count;
}

完整程式碼如下:

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

char *strtok_implementation(char *, const char *);
int strlen_implemention(const char *);

int main() {

	char a[] = "Hello there haha yo";
	char *tokenPtr;

	cout << "Original string a:\n" << a << endl;
	cout << endl;

	cout << "strlen_implemention(a):" << strlen_implemention(a) << endl;
	cout << endl;

	cout << "strtok_implementation(a):"<<endl;
	tokenPtr = strtok_implementation(a," ");

	while (tokenPtr != NULL)
	{
		cout << tokenPtr << endl;
		tokenPtr = strtok_implementation(NULL," ");
	}

	cout << "\nAfter strtok, sentence = " << a << endl;

	system("pause");
	return 0;
}

char *strtok_implementation(char *s, const char *delim)
{
	char *spanp;
	int c, sc;
	char *tok;
	static char *last;


	if (s == NULL && (s = last) == NULL)
	{
		return NULL;
	}

	c = *s++;
	for (spanp = (char*)delim; (sc = *spanp++); ) {
		if (c == sc) {
			c = *s++;
			spanp = (char*)delim;
		}
	}

	if (c == 0) {		
		last = NULL;
		return (NULL);
	}
	tok = s - 1;


	while(1) {
		c = *s++;
		spanp = (char *)delim;
		do {
			if ((sc = *spanp++) == c) 
			{
				if (c == 0)
				{
					s = NULL;
				}
				else
				{
					s[-1] = 0;
				}
				last = s;
				return (tok);
			}
		} while (sc != 0);
	}
}

int strlen_implemention(const char *s)
{
	int count = 0;
	for (;*s!='\0';count++,s++)
	{

	}

	return count;
}

實際執行結果如下:

有夢最美 築夢踏實

活在當下 認真過每一天

我是阿夢 也是Ace