cracking the coding interview No1.2



1.2Implement a function vod reverse(char *str)in C or C++which reverses a null-terminated string.


void reverse(char *str)
{
	if (str == NULL)
		return;

	char *str1 = str;
	int length = 0;
	while (*str1!='\0')
	{
		length++;
	}

	for (int i = 0;i < length/2; i++)
	{
		int temp = *(str + i);
		*(str + i) = *(str + length - i - 1);
		*(str + length - i - 1) = temp;
	}
}

你可能感兴趣的:(cracking the coding interview No1.2)