摘要:[C++] 字串反轉
[C++] 字串反轉
01
#include <stdio.h>
02
#include <stdlib.h>
03
#include <string.h>
04
#include <iostream>
09
using namespace std;
10
11
void reverse(const char *str,char *str2)
12
{
13
int i=0;
14
int j=strlen(str)-1;
15
//char temp;
16
while(j>=0)
17
{
18
str2[i]=str[j];
19
i++;
20
j--;
21
}
22
str2[i]='\0';
23
}
24
25
int main(void)
26
{
27
char *p_str[10]; //宣告一個二維指標字元陣列
28
p_str[0]="hello";
29
p_str[1]="world game";
30
p_str[2]=(char *)malloc(sizeof(char)*15);
31
p_str[3]=(char *)malloc(sizeof(char)*15);
32
reverse(p_str[0],p_str[2]); // 將資料反轉
33
reverse(p_str[1],p_str[3]);
34
cout << p_str[2] << endl;
35
cout << p_str[3] << endl;
36
free(p_str[2]);
37
free(p_str[3]);
38
system("pause");
39
return 0;
40
}
#include <stdio.h> 02
#include <stdlib.h> 03
#include <string.h> 04
#include <iostream> 09
using namespace std; 10
11
void reverse(const char *str,char *str2) 12
{ 13
int i=0; 14
int j=strlen(str)-1; 15
//char temp; 16
while(j>=0) 17
{ 18
str2[i]=str[j]; 19
i++; 20
j--; 21
} 22
str2[i]='\0'; 23
} 24
25
int main(void) 26
{ 27
char *p_str[10]; //宣告一個二維指標字元陣列 28
p_str[0]="hello"; 29
p_str[1]="world game"; 30
p_str[2]=(char *)malloc(sizeof(char)*15); 31
p_str[3]=(char *)malloc(sizeof(char)*15); 32
reverse(p_str[0],p_str[2]); // 將資料反轉 33
reverse(p_str[1],p_str[3]); 34
cout << p_str[2] << endl; 35
cout << p_str[3] << endl; 36
free(p_str[2]); 37
free(p_str[3]); 38
system("pause"); 39
return 0; 40
}
