Last active
August 29, 2015 14:06
-
-
Save maplebeats/f6893f60ddadd7d83645 to your computer and use it in GitHub Desktop.
反转字符串原地操作版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
int strlen(char * str) | |
{ | |
int i=0; | |
while(str[i]!='\0') | |
i++; | |
return i; | |
} | |
int strcpy(char *Rstr, char *Dstr) | |
{ | |
int i; | |
for(i=0;i<strlen(Rstr);i++) | |
{ | |
Dstr[i]=Rstr[i]; | |
} | |
return 0; | |
} | |
void rever(char *Pstr,int lptr, int rptr) | |
{ | |
int width=rptr-lptr+1, i; | |
for(i=0;i<(width/2);i++) | |
{ | |
char tmp; | |
tmp = Pstr[lptr+i]; | |
Pstr[lptr+i]=Pstr[rptr-i]; | |
Pstr[rptr-i]=tmp; | |
} | |
} | |
int reverse(char *Pstr, int len) | |
{ | |
rever(Pstr, 0, len-1); | |
int lptr=0, rptr, i; | |
for(i=0;i<=len;i++) | |
{ | |
if(Pstr[i]==' ' || Pstr[i]=='\0'){ | |
rptr = i-1; | |
rever(Pstr, lptr, rptr); | |
lptr=rptr+2; | |
} | |
} | |
return rptr; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
assert(argc!=1); | |
char * str = argv[1]; | |
int len = strlen(str); | |
char * string = (char *) malloc(len); | |
strcpy(str, string); | |
int r = reverse(string, len); | |
printf("rptr:%d\n", r); | |
printf("string:%s\n", string); | |
free(string); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment