我正在學習C編程方法,我有一個關於循環的問題。 我正在使用linux debian進行編譯。while循環在for循環後不執行
這裏是代碼。
#include <stdio.h>
int main()
{
char s1[] = "abcdef";
char s2[] = "aaaaaa";
int i, j;
printf("\ndebut:%s\n", s1);
for (i = j = 0; s1[i] != '\0'; ++i, ++j){
if (s1[i] == s2[j]){
printf("before %c\n", s1[i]);
s1[i] = '\0';
}
else if (s1[i] != s2[j]){
s1[i] = s1[i];
printf("after copy %c\n", s1[i]);
}
}
printf("\nEnd of for loop write final result\n.");
i = j = 0;
while (s1[i] != '\0'){
printf("final is :%c ", s1[i]);
++i;
}
return 0;
}
這段代碼假設一開始信刪除一個字母「a」在我的例子,然後,while循環會通每個位置打印結果。
它看起來像第一部分工作,但while循環永遠不會被程序執行。
有什麼想法爲什麼?
謝謝
那麼,你的循環條件評估爲false。所以有時間傾斜如何使用調試器並檢查變量。 – OldProgrammer
空字符表示字符串在該位置結束,而不是它應該被視爲跳過字符。我不確定你的代碼是否假設。 – Jems
這個問題似乎是無關緊要的,因爲它沒有顯示任何調試嘗試。 – 2014-01-26 21:22:40