要求:不允许使用库函数(strlen,strcpy等)
我们首先要创建一个可以计算字符串长的函数,在opposite中创建两个指针分别指向字符串的左右两端,不断的交换最后实现反转
代码如下:
#include <stdio.h>
#include <stdlib.h>
int my_strlen(char*x)
{
int count = 0;
while(*x != '\0')
{
count++;
x++;
}
return count;
}
void opposite(char*arr)
{
char *left = arr;
char *right = arr + my_strlen(arr) - 1;
char temp = 0;
while(left < right)
{
temp = *left;
*left = *right;
*right = temp;
*left++;
*right--;
}
}
int main()
{
char arr[] = "abcdef";
opposite(arr);
printf("%s\n",arr);
}