当前位置: 移动技术网 > 移动技术>移动开发>Android > 用指针将字符串数组位置颠倒

用指针将字符串数组位置颠倒

2020年07月13日  | 移动技术网移动技术  | 我要评论

代码文件:代码下载

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

void show(const char* const str);
char* Position_swap(char* const str);

int main(int argc, char* argv[])
{
	char str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	printf("Before the swap:");
	show(str);
	Position_swap(str);
	printf("After  the swap:");
	show(str);
	return 0;
}
/**********************************************
*	头文件:#include <stdio.h>
*	功	能:打印字符数组信息
*	函	数:void show(char * str)
*	参	数:str字符数组首地址
*	返回值:无
**********************************************/
void show(const char* const str)
{
	int len = strlen(str);
	int i = 0;
	while (i <= len)
	{
		printf("%c",*(str + i++));
	}
	printf("\n");
}
/**********************************************
*	头文件:#include <string.h>
*	功	能:实现字符数组位子互换
*	函	数:char* Position_swap(char * str)
*	参	数:str字符数组首地址
*	返回值:str字符数组首地址
**********************************************/
char* Position_swap( char * const str)
{
	int len = strlen(str);
	int i = 0, j = len-1;
	char ch = 0;
	while (i <= j)
	{
		ch = *(str + i); 
		*(str + i) = *(str + j);
		*(str + j) = ch;
		i++;
		j--;
	}
	return str;
}

本文地址:https://blog.csdn.net/weixin_42059464/article/details/107280473

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网