In this recipe, we will learn to reverse a string using pointers. The best part is that we will not reverse the string and copy it onto another string, but we will reverse the original string itself.
Reversing a string using pointers
How to do it…
- Enter a string to assign to the str string variable as follows:
printf("Enter a string: ");
scanf("%s", str);
- Set a pointer to point at the string, as demonstrated in the following code. The pointer will point at the memory address of the string's first character:
ptr1=str;
- Find the length of the string by initializing an n variable to 1. Set a while loop to execute when the pointer reaches the null character of the...