Re: [問題] 二維字元陣列 & 字串反轉

看板C_and_CPP作者 (忠言本逆耳,實願君向善.)時間14年前 (2009/09/13 01:26), 編輯推噓3(300)
留言3則, 3人參與, 最新討論串2/2 (看更多)
43 我寫成下面這樣,有做些修改. #include <stdio.h> #include <stdlib.h> #include <string.h> void reverse(const char *str,char *str2) { int i=0; int j=strlen(str)-1; char c; while (j>=0) { str2[i] = str[j]; i++, j--; } str2[i]='\0'; } int main() { char *p_str[10]; //宣告一個二維指標字元陣列 p_str[0]="hello"; p_str[1]="world game"; p_str[2]=(char *)malloc(sizeof(char)*15); p_str[3]=(char *)malloc(sizeof(char)*15); reverse(p_str[0],p_str[2]); // 將資料反轉 reverse(p_str[1],p_str[3]); printf("%s\n",p_str[2]); printf("%s\n",p_str[3]); free(p_str[2]); free(p_str[3]); system("pause"); return 0; } 用Dev-C++ 可執行. -- 學長學長!那邊有飆車族 學長學長!那邊剛好像有女生 學長學長~那邊有人紅燈右轉 砍人 被壓上車 ψQSWEET 鴿 鴿 鴿 鴿 鴿他媽的 鴿 ◎ ◎ 喔~~ ︶ ︶ ◎ ◎ 喔~~ ︶ ︶ ◎ ◎ 攔下來呀! ⊙◥ 3╯ξ 沒王法了 (哈欠) (煙~) 是不是?!( ) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.194.100.216

09/13 01:37, , 1F
印象中字串反轉有現成的標準函式可用
09/13 01:37, 1F

09/13 03:03, , 2F
char *strrev(const char*);
09/13 03:03, 2F

09/14 00:01, , 3F
感謝
09/14 00:01, 3F
文章代碼(AID): #1Agzd13T (C_and_CPP)
文章代碼(AID): #1Agzd13T (C_and_CPP)