以下是字符串逆置
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "time.h" #include <sys/stat.h> void inverse(char *str)//字符串逆置 { char *p1 = str; char *p2 = str + strlen(str) - 1; while (p1 < p2) { char tmp = *p1; *p1 = *p2; *p2 = tmp; p1++; p2--; } } int main() { char buf[] = "abcdefg"; inverse(buf); printf("buf=%s\n", buf); } void inverse(char *str,int num)//字符串逆置指定逆直前几位 { char *p1 = str; char *p2 = str + num - 1; while (p1 < p2) { char tmp = *p1; *p1 = *p2; *p2 = tmp; p1++; p2--; } }