Sort a string in reverse order

//There were always errors at the beginning, and the reverse order could not be printed. It turned out to be because it sizeof(src)/sizeof(src[0]);was used find the length of the string.

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

char *reserve(char *src)
{
    
    
	int i;
	int len = strlen(src);
	int n = len/2;	
	char tmp;

	for(i = 0;i < n; i++){
    
    
		tmp = src[i];
        src[i] = src[len-i-1];
        src[len-i-1] = tmp;
	}

	return src; 
}
int main()
{
    
    
	char str[20];

    printf("Please input a string:\n");

    scanf("%s",str);
	
    printf("The reserved string is:\n%s",reserve(str));

	return 0;
}

Guess you like

Origin blog.csdn.net/lijunlin0329/article/details/129064618