刪除字符串中的“b”和“ac”

刪除字符串中的“b”和“ac”,需要滿足如下的條件:字符串只能遍歷一次;不能夠實用額外的空間。例如:acbac ==> "";aaac ==> aa;ababac ==> aa;bbbbd ==> d

 

#include <iostream>
using namespace std;

void delete_chars(char * str)
{
	int location = 0;

	int i =0 ;
	while( str[i] != '\0'){

		if(str[i] != 'b')
		{
			str[location] = str[i];
			if(location == 0){
				location++;
				i++;
				continue;
			}
			if (str[location] == 'c' && str[location -1] == 'a' )
			{
				location--;
			}
			else
				location++;

		}
		
		i++;

	}

	str[location] = '\0';



}

int main()
{
	char s[20] = "caaccbacd";
	delete_chars(s);
	cout<<s<<endl;
	system("pause()");
	return 0;
}


 

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