字符串反转

今天遇到一个题,让反转字符串,简单题,注意下细节就好了

#include <stdio.h>
#include <string.h>

char *str_reverse(char *str) {
	if (str == NULL) {
		return NULL;
	}
	for (char *left=str, *right=str+strlen(str)-1;
		left<right; left++, right--) {
		char tmp = *left;
		*left = *right;
		*right = tmp;
	}
	return str;
}

int main() {
	char str[] = "123456";
	printf("%sn", str_reverse(str));
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *