求大神,哪里錯了?
#include <stdio.h>
#include <string.h>
int main(void)
{
char word[10];
int length, index, temp;
printf ("please Enter a word:\n");
while (scanf("%s", word)==1)
{
length = strlen(word);//倒序打印
for(index=0; index<=(length+index)/2; index++) //二分 交換?
{
temp = word[index];
word[index] = word[length-index];
word[length-index] = temp;
}
printf ("%s", word);
printf ("Do you want to continue?\n");
}
return 0;
}
為什么沒交換?
2017-01-20
//倒序打印
for(index=0; index<=length/2-1; index++) ? ? ? ?//二分 交換?
{
????temp = word[index];
????word[index] = word[length-index-1];
????word[length-index-1] = temp;
}
1、一個基本的錯誤為:沒有意識到數組的下標是位于[0,length),所以導致出現了后面兩個問題:
2、二分交換的結束值的取值有問題。應該是length/2-1,而不是(length+index)/2
3、交換時選擇的下標有問題,應該是length-index-1,而不是length-index
2020-11-09
講的不錯,受益匪淺,本人自己總結了C語言的基本學習知識,函數語法 數據結構 鏈表 數組 指針等視頻合集給大家提供學習,需要學習的,包括有學習相關問題,可以進入(裙:834597065)交流,里面有眾多大佬幫助解決學習、技術問題,只針對C/C++問題哦