C Language, C++

Reverse a string in c without using library function

aticleworld.com

In the interview generally, Interviewer asked the question to reverse a string without the using the c library function or maybe they can mention more condition, it totally depends on the interviewer.



In this article, I will discuss several techniques to reverse a string. I hope you guys like it. Here I am assuming you guys already familiar with string but if you don’t have any idea about the string then don’t need to worry. I am giving here the small introduction of the string.

A string is the collection of characters and it terminate always with a null character means that every string contains a null character at the end of the string.

Example:

char *pszData = “aticle”;
In above example, pszData is the pointer to the string. All characters of the string are stored in a contiguous memory and consist a null character in the last of the string.

See the bellow table:

character  ‘a’ ‘t’ ‘i’ ‘c’ ‘l’ ‘e’ ‘\0’
Address  0x00 0x01 0x02 0x03 0x04 0x05 0x06

Here we will reverse a string using two methods, iterative and recursive.

Iterative method

An iterative method is the simplest way to reverse a string in c. In this article, I am discussing few method to reverse a string in c.

Algorithm:

  • Calculate the length (Len) of string.
  • Initialize the indexes of the array.
    Start = 0, End = Len-1
  • In a loop, swap the value of pszData[Start] with pszData[End].
  • Change the indexes’ of the array as follows.
    Start = start +1; End = end – 1


Reverse a string using the temporary variable

OutPut:

Explanation of the program:

Firstly find the length of string

while(acData[iLen++] != ‘\0’);

we know that string contains a null character, so remove the null character.
iLen–;

Let suppose “ABCD” is the string, so when we try to calculate the length of the string using the above expression then iLen also counts the null character. So here I am reducing the iLen to exclude the null character.

while(acData[iLen++] != ‘\0’);
iLen = 5;
iLen–;
iLen = 4;

We know that a string is character array and it has a range between 0 to (iLen-1);

iLen–; // So for the index of array
iLen = 3;
Now we have the starting and ending location of the character in the string.
iLoop =0;
iLen =3;

‘iLoop’ is first positioned character and ‘iLen’ is positioned off the last character. Now we are swapping characters at position ‘iLoop’ and ‘iLen’. After swapping the characters we will increment the value of ‘iLoop’ and decrement the value of ‘iLen’.

Reverse a string using an EX_OR operator

This method is similar to the above program, in which we just use EX-OR operator to swap the character beside the third variable.

OutPut:

Reverse a string using an extra buffer.

This is the very simple method to reverse a string. In this method, we use an extra buffer to stored the reverse string.

OutPut:

 Reverse a string using two pointers

Here we are using two pointers for the character swapping.

OutPut:


Reverse a string using the macro 

This method is also similar to the above describe method. In which we create a macro and pass the starting and ending address of the string and performed the swap operation.

Output:

 Reversing a string using single a pointer

OutPut:



Recursive Way to reverse a string

  • Calculate the length (Len) of string.
  • Initialize the indexes of the array.
    Start = 0, End = Len-1
  • swap the value of pszData[Start] with pszData[End].
  • Change the indexes’ of an array as below and Recursively call reverse function for the rest array.
    Start = start +1; End = end – 1

Method 1:

OutPut:

 

Method 2:

In this method, a reverse function does not insert the null character in the last of the string. So before using this method you have to insert the null character manually in the last of the string.

OutPut:

 


2 Comments

  1. prathima

    Sir can u post about circular linked list

    • Amlendra

      Yes, In future I will write an article on circular linked list.

Leave a Reply

Theme by Anders Norén