How it works...

We will be prompted to enter a string that will be assigned to the str variable. A string is nothing but a character array. Assuming we enter the name manish, each character of the name will be assigned to a location in the array one by one (see Figure 4.4). We can see that the first character of the string, the letter m, is assigned to the str[0] location, followed by the second string character being assigned to the str[1] location, and so on. The null character, as usual, is at the end of the string, as shown in the following diagram:

Figure 4.4

To reverse the string, we will seek the help of two pointers: one will be set to point at the first character of the string, and the other at the final character of the string. So, the first ptr1 pointer is set to point at the first character of the string as follows:

Figure 4.5

The exchanging of the characters has to be executed equal to half the length of the string; therefore, the next step will be to find the length of the string. After finding the string's length, the ptr1 pointer will be set to move to the final character of the string.

In addition, another ptr2 pointer is set to point at m, the first character of the string, as shown in the following diagram:

Figure 4.6

The next step is to interchange the first and last characters of the string that are being pointed at by the ptr1 and ptr2 pointers (see Figure 4.7 (a)). After interchanging the characters pointed at by the ptr1 and ptr2 pointers, the string will appear as shown in Figure 4.7 (b):

Figure 4.7

After interchanging the first and last characters, we will interchange the second and the second to last characters of the string. To do so, the ptr2 pointer will be moved forward and set to point at the next character in line, and the ptr1 pointer will be moved backward and set to point at the second to last character.

You can see in the following Figure 4.8 (a) that the ptr2 and ptr1 pointers are set to point at the a and s characters. Once this is done, another interchanging of the characters pointed at by ptr2 and ptr1 will take place. The string will appear as follows (Figure 4.8 (b)) after the interchanging of the a and s characters: 

Figure 4.8

The only task now left in reversing the string is to interchange the third and the third to last character. So, we will repeat the relocation process of the ptr2 and ptr1 pointers. Upon interchanging the n and i characters of the string, the original str string will have been reversed, as follows:

Figure 4.9

After applying the preceding steps, if we print the str string, it will appear in reverse.

Let's use GCC to compile the reversestring.c program as follows:

D:CBook>gcc reversestring.c -o reversestring

If you get no errors or warnings, that means the reversestring.c program has been compiled into an executable file, called reversestring.exe. Let's run this executable file as follows:

D:CBook>./reversestring
Enter a string: manish
Reverse string is hsinam

VoilĂ ! We've successfully reversed a string using pointers. Now, let's move on to the next recipe!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset