|
- #include <stdio.h>
- #include <iostream>
- void reverse_str(char * ch);
- void reverse_str2(char *ch);
- int main(void)
- {
- char c[] = "Where there is a will, there is a way!";
- printf("original string c: \n%s\n", c);
- reverse_str(c);
- printf("reversed string after calling reverse_str: \n%s\n", c);
- reverse_str2(c);
- printf("reversed string after calling reverse_str2: \n%s\n", c);
- getchar();
- return 0;
- }
- void reverse_str(char *ch) /*使用中间变量*/
- {
- int len;
- int i;
- len = strlen(ch)-1;
- char ctemp;
- for(i = 0; i < len-i; i++)
- {
- ctemp = ch;
- ch = ch[len-i];
- ch[len-i] = ctemp;
- }
- }
- void reverse_str2(char *ch) /*不用中间变量*/
- {
- int len;
- int i;
- len = strlen(ch)-1;
- char ctemp;
- for(i = 0; i < len-i; i++)
- {
- ch = ch ^ ch[len-i];
- ch[len-i] = ch ^ ch[len-i];
- ch = ch ^ ch[len-i];
- }
- }
复制代码 |
|