溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么實(shí)現(xiàn)幾個(gè)字符串常用函數(shù)

發(fā)布時(shí)間:2021-10-14 10:38:49 來源:億速云 閱讀:129 作者:柒染 欄目:編程語言

怎么實(shí)現(xiàn)幾個(gè)字符串常用函數(shù),相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

實(shí)現(xiàn)幾個(gè)字符串常用函數(shù),練習(xí)一下寫代碼。經(jīng)常謝謝代碼,使自己不要忘了如何寫代碼。

字符比較函數(shù)

字符串賦值函數(shù)

求字符串長度

字符串那倒置

字符串比較

字符串連接

// string.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <assert.h>
#include "string.h"

//字符交換函數(shù)
void charswap(char& ch2, char& ch3)
{
	char ch = ch2;
	ch2 = ch3;
	ch3 = ch;
}

//求字符串長度
int stringlength(const char* sourstr)
{
	const char *pstr = sourstr;
	int length = 0;
	while(*pstr++ != '\0')
	{
		length++;
	}
	return length;
}

//字符串那倒置
char* stringconvert(char* sourstr)
{
	int length = stringlength(sourstr);
	int loopnumber = length / 2;
	int index = 0;
	while(index < loopnumber)
	{
		charswap(*(sourstr + index), *(sourstr + length - index - 1));
		index++;
	}
	return sourstr;
}

//字符串復(fù)制
char* stringcopy(char* deststr, const char* sourstr)
{
	const char* pstr = sourstr;
	int index = 0;
	while(*pstr != '\0')
	{
		*(deststr + index) = *pstr++;
		index++;
	}
	*(deststr + index) = '\0';
	return deststr;
}

//字符串連接
char* stringcontact(char* deststr, const char* sourstr)
{
	const char* pstr = sourstr;
	int length = stringlength(deststr);
	int index = 0;
	while(*pstr != '\0')
	{
		*(deststr + length + index) = *pstr++;
		index++;
	}
	*(deststr + length + index ) = '\0';
	return deststr;
}

//字符串比較函數(shù)
int stringcompare(const char* deststr, const char* sourstr)
{
	const char* pdest = deststr;
	const char* psour = sourstr;

	while(*pdest == *psour && *pdest != '\0')
	{
		pdest++;
		psour++;
	}
	return *pdest - *psour;
}


int main(int argc, char* argv[])
{ 
	char buff1[100];
	char buff2[100];
	stringcopy(buff1, "reqre12345");
	stringcopy(buff2, "reqre1");

	printf("%d\n", stringcompare(buff1, buff2));

	getchar();

	return 0;
}

看完上述內(nèi)容,你們掌握怎么實(shí)現(xiàn)幾個(gè)字符串常用函數(shù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI