1.2

Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)

void reverse(char* str){
	if(str==NULL)
	    return;
	int i=0,j=strlen(str)-1;
	while(i<j){
		int tmp=str[i];
		str[i]=str[j];
		str[j]=tmp;
		//forget i++,j-- when writing on paper
		i++;
		j--;
	}
}
在紙上,看似簡單的代碼還是總出問題:我忘記了i++,j--,將紙上的程序輸入到計算機中時導致死循環
發佈了286 篇原創文章 · 獲贊 8 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章