3
#include<conio.h>
#include<stdio.h>
int main(void)
{
char str[20];
char *ptr1,*ptr2;
printf("Enter string\n");
gets(str);
ptr1,ptr2=&str[0];
while(*ptr2!='\0')
{
ptr2++;
}
ptr2--;
printf("rev_string =");
while(ptr1!=ptr2) //this should work for when strlen=odd integer
{
int temp=*ptr2;
*ptr2=*ptr1;
*ptr1=temp;
ptr1++;
ptr2--;
}
puts(str);
return 0;
}
什麼錯我的代碼?我知道我已經投入了while循環condtion是不是要去工作時的字符串的長度是偶數,但它扭轉一個字符串就地應該適用於奇怪的情況。嘗試使用兩個指針
是的,我已經提到,條件(PTR1!= PTR2)不是連長度會工作字符串,但它應該適用於奇數,只是想知道爲什麼它也不適用於奇數長度。 –
@Peince Vijay Pratap查看我更新的帖子。 –
你能告訴我爲什麼你這樣做嗎? if(ptr2!= str && *(ptr2 - 1)=='\ n')* - ptr2 ='\ 0'; –