從字符串的指定位置開始,刪除指定長度字符


//領會指針和其它內存操作的技巧
char *deleteChars(char *str,int pos,int len)
{
	char *p = str + pos -1;//指向pos位置字符
	int tt = strlen(str);
	if (pos < 1 || (p-str) > tt)
	{
		return str;
	}
	if( (p+len-str) > tt)//len大於pos後剩餘的字符個數,只需對pos位置賦 '\0',因爲超出長度相當於刪除後面所有的字符
	{
		*p = '\0';
		return str;
	}
	while (*p && *(p+len) )//len小於或等於pos後剩餘的字符個數,刪除中間len個字符
	{
		*p = *(p+len);//通過指針直接修改
		p++;
	}
	*p = '\0';
	return str;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章