C Program to Reverse a String without using String Functions

Write a c program to reverse a string without using string functions.

For example,

Input
JohnSmith

Output
htimSnhoJ

The simplest way to reverse a string in C is to use strrev() function. But in this article, we will learn how we can reverse a string without using strrev() or any other standard library function.

We can reverse a given string by simply swapping ith character from the start and ith character from the end of the string. Each pair of characters is only swapped once. If we swap a pair twice then we will obtain the same string.

See the following illustration for better understanding.

reverse string without strrev()

Steps

  1. Let N be the size of String.
  2. Set i = 0 and j = N – 1
  3. Repeat the following steps while i < j
    • Swap(string[i] , string[j])
    • ++i
    • –j

We can get the size of string using strlen() function but we are not supposed to use any library function.

We will implement our own strlen() function. In C/C++, every string ends with a null character ‘\0’. To calculate the size of the string, we will traverse each character of the array until we reach ‘\0’ and use a variable ‘count’ to count the number of characters in the string.

See the following illustration for better understanding.

length of array with strlen

Steps

  1. Set count = 0.
  2. Repeat while string[count] != ‘\0’
    • ++count
  3. Return count

Program to Reverse String in C without String functions

#include<stdio.h>

// function to find the length of string
int length(char* string) {

	int count = 0;

	// increment count until we reach the null charcter '\0'
	// null character marks the end of string
	while (string[count] != '\0') {
		++count;
	}
	return count;

}

// function to reverse string
void reverse(char* string) {

	char temp;
	int i, j, n;
	n = length(string);

	// swapping string[i] and string[j] until i<j
	for (i = 0, j = n - 1; i < j; ++i, --j) {
		temp = string[j];
		string[j] = string[i];
		string[i] = temp;
	}

}

int main()
{
	char string[100];
	printf("Enter a String: ");
	scanf("%s", string);

	printf("\nString Before Reverse: %s", string);
	reverse(string);
	printf("\n\nString After Reverse: %s", string);
}

Output

output

We can also implement the program using recursion.

Recursive Implementation

The recursive call accepts 3 parameters – i, j, string.
i and j are the index we need to swap. j is basically the index of ith character from last. After swapping, we increment i and decrement j and call the function recursively.

Steps

  1. If i > j
    • it means we have already swapped all pair of characters
    • Return
  2. Swap(string[i], string[j])
  3. reverse(i+1, j-1, string)

Program

#include<stdio.h>

// function to find the length of string
int length(char* string) {
	int count = 0;
	// increment count until we reach the null character '\0'
	// null character marks the end of string
	while (string[count] != '\0') {
		++count;
	}
	return count;
}

// recursive function to reverse string
// each recursive call swaps each pair of number
// j is basically the ith index from last
void reverse(int i, int j, char* string) {
	// i>j means we have swapped all required pairs
	if (i > j)
		return;
	// swapping string[i] and string[j]
	int temp = string[i];
	string[i] = string[j];
	string[j] = temp;
	// calling reverse function recursively to swap next pair of characters
	reverse(i + 1, j - 1, string);
}

int main()
{
	char string[100];
	printf("Enter a String: ");
	scanf("%s", string);
	printf("\nString Before Reverse: %s", string);
	reverse(0, length(string) - 1, string);
	printf("\n\nString After Reverse: %s", string);
}

Output

reverse string using recursion

Read
Left Shit Array by One
Print Subset of Array in C++
Student Management Program in C

Leave a Comment

Your email address will not be published. Required fields are marked *