Skip to main content

Reverse a string without affecting special characters

Here given code implementation process.

/*
    C program for
    Reverse a string without affecting special characters
*/
#include <stdio.h>

#include <string.h>

int isAlphabet(char value)
{
	if ((value >= 'a' && value <= 'z') || 
        (value >= 'A' && value <= 'Z'))
	{
		return 1;
	}
	return 0;
}
void reverseAlphabet(char *text)
{
	// Get number of characters in text
	int n = strlen(text);
	// Before replace text
	printf(" Before : %s", text);
	int low = 0;
	int high = n - 1;
	while (low < high)
	{
		if (isAlphabet(text[low]) == 1 && isAlphabet(text[high]) == 1)
		{
			// Swap to alphabets
			char temp = text[low];
			text[low] = text[high];
			text[high] = temp;
			low++;
			high--;
		}
		else
		{
			if (isAlphabet(text[low]) == 0)
			{
				low++;
			}
			if (isAlphabet(text[high]) == 0)
			{
				high--;
			}
		}
	}
	// After replace text
	printf("\n After : %s \n", text);
}
int main(int argc, char
	const *argv[])
{
	char text1[] = "(e)x>chan?gi@n!g";
	char text2[] = "[M&a-nage]";
	char text3[] = "abcd";
	// Test case
	reverseAlphabet(text1);
	reverseAlphabet(text2);
	reverseAlphabet(text3);
	return 0;
}

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
 Before : abcd
 After : dcba
/*
    Java Program for
    Reverse a string without affecting special characters
*/
public class ReverseOperation
{
	public boolean isAlphabet(char value)
	{
		if ((value >= 'a' && value <= 'z') || 
            (value >= 'A' && value <= 'Z'))
		{
			return true;
		}
		return false;
	}
	public void reverseAlphabet(String text)
	{
		// Get number of characters in text
		int n = text.length();
		// Before replace text
		System.out.print(" Before : " + text);
		int low = 0;
		int high = n - 1;
		String first = "";
		String last = "";
		while (low < high)
		{
			if (isAlphabet(text.charAt(low)) == true &&
                isAlphabet(text.charAt(high)) == true)
			{
				// Both char is Alphabet
				first = first + text.charAt(high);
				last = text.charAt(low) + last;
				low++;
				high--;
			}
			else
			{
				if (isAlphabet(text.charAt(low)) == false)
				{
					first = first + text.charAt(low);
					low++;
				}
				if (isAlphabet(text.charAt(high)) == false)
				{
					last = text.charAt(high) + last;
					high--;
				}
			}
		}
		first += last;
		// Display result
		System.out.print("\n After : " + (first) + " \n");
	}
	public static void main(String[] args)
	{
		ReverseOperation task = new ReverseOperation();
		// Test A
		task.reverseAlphabet("(e)x>chan?gi@n!g");
		// Test B
		task.reverseAlphabet("[M&a-nage]");
	}
}

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Reverse a string without affecting special characters
*/
class ReverseOperation
{
	public: bool isAlphabet(char value)
	{
		if ((value >= 'a' && value <= 'z') || 
            (value >= 'A' && value <= 'Z'))
		{
			return true;
		}
		return false;
	}
	void reverseAlphabet(string text)
	{
		// Get number of characters in text
		int n = text.length();
		// Before replace text
		cout << " Before : " << text;
		int low = 0;
		int high = n - 1;
		string first = "";
		string last = "";
		while (low < high)
		{
			if (this->isAlphabet(text[low]) == true 
                && this->isAlphabet(text[high]) == true)
			{
				// Both char is Alphabet
				first = first  +  text[high];
				last = text[low]  +  last;
				low++;
				high--;
			}
			else
			{
				if (this->isAlphabet(text[low]) == false)
				{
					first = first  +  text[low];
					low++;
				}
				if (this->isAlphabet(text[high]) == false)
				{
					last = text[high] +  last;
					high--;
				}
			}
		}
		first += last;
		// Display result
		cout << "\n After : " << (first) << " \n";
	}
};
int main()
{
	ReverseOperation *task = new ReverseOperation();
	// Test A
	task->reverseAlphabet("(e)x>chan?gi@n!g");
	// Test B
	task->reverseAlphabet("[M&a-nage]");
	return 0;
}

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
// Include namespace system
using System;
/*
    Csharp Program for
    Reverse a string without affecting special characters
*/
public class ReverseOperation
{
	public Boolean isAlphabet(char value)
	{
		if ((value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z'))
		{
			return true;
		}
		return false;
	}
	public void reverseAlphabet(String text)
	{
		// Get number of characters in text
		int n = text.Length;
		// Before replace text
		Console.Write(" Before : " + text);
		int low = 0;
		int high = n - 1;
		String first = "";
		String last = "";
		while (low < high)
		{
			if (this.isAlphabet(text[low]) == true && 
                this.isAlphabet(text[high]) == true)
			{
				// Both char is Alphabet
				first = first + text[high];
				last = text[low] + last;
				low++;
				high--;
			}
			else
			{
				if (this.isAlphabet(text[low]) == false)
				{
					first = first + text[low];
					low++;
				}
				if (this.isAlphabet(text[high]) == false)
				{
					last = text[high] + last;
					high--;
				}
			}
		}
		first += last;
		// Display result
		Console.Write("\n After : " + (first) + " \n");
	}
	public static void Main(String[] args)
	{
		ReverseOperation task = new ReverseOperation();
		// Test A
		task.reverseAlphabet("(e)x>chan?gi@n!g");
		// Test B
		task.reverseAlphabet("[M&a-nage]");
	}
}

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
package main

import "fmt"
/*
    Go Program for
    Reverse a string without affecting special characters
*/

func isAlphabet(value byte) bool {
	if (value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z') {
		return true
	}
	return false
}
func reverseAlphabet(text string) {
	// Get number of characters in text
	var n int = len(text)
	// Before replace text
	fmt.Print(" Before : ", text)
	var low int = 0
	var high int = n - 1
	var first string = ""
	var last string = ""
	for (low < high) {
		if isAlphabet(text[low]) == true && isAlphabet(text[high]) == true {
			// Both char is Alphabet
			first = first + string(text[high])
			last = string(text[low]) + last
			low++
			high--
		} else {
			if isAlphabet(text[low]) == false {
				first = first + string(text[low])
				low++
			}
			if isAlphabet(text[high]) == false {
				last = string(text[high]) + last
				high--
			}
		}
	}
	first += last
	// Display result
	fmt.Print("\n After : ", (first), " \n")
}
func main() {
	
	// Test A
	reverseAlphabet("(e)x>chan?gi@n!g")
	// Test B
	reverseAlphabet("[M&a-nage]")
}

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
<?php
/*
    Php Program for
    Reverse a string without affecting special characters
*/
class ReverseOperation
{
	public	function isAlphabet($value)
	{
		if (($value >= 'a' && $value <= 'z') || 
            ($value >= 'A' && $value <= 'Z'))
		{
			return true;
		}
		return false;
	}
	public	function reverseAlphabet($text)
	{
		// Get number of characters in text
		$n = strlen($text);
		// Before replace text
		echo(" Before : ".$text);
		$low = 0;
		$high = $n - 1;
		$first = "";
		$last = "";
		while ($low < $high)
		{
			if ($this->isAlphabet($text[$low]) == true 
                && $this->isAlphabet($text[$high]) == true)
			{
				// Both char is Alphabet
				$first = $first.$text[$high];
				$last = $text[$low].$last;
				$low++;
				$high--;
			}
			else
			{
				if ($this->isAlphabet($text[$low]) == false)
				{
					$first = $first.$text[$low];
					$low++;
				}
				if ($this->isAlphabet($text[$high]) == false)
				{
					$last = $text[$high].$last;
					$high--;
				}
			}
		}
		$first .= $last;
		// Display result
		echo("\n After : ".($first)." \n");
	}
}

function main()
{
	$task = new ReverseOperation();
	// Test A
	$task->reverseAlphabet("(e)x>chan?gi@n!g");
	// Test B
	$task->reverseAlphabet("[M&a-nage]");
}
main();

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
/*
    Node JS Program for
    Reverse a string without affecting special characters
*/
class ReverseOperation
{
	isAlphabet(value)
	{
		if ((value >= 'a' && value <= 'z') || 
            (value >= 'A' && value <= 'Z'))
		{
			return true;
		}
		return false;
	}
	reverseAlphabet(text)
	{
		// Get number of characters in text
		var n = text.length;
		// Before replace text
		process.stdout.write(" Before : " + text);
		var low = 0;
		var high = n - 1;
		var first = "";
		var last = "";
		while (low < high)
		{
			if (this.isAlphabet(text.charAt(low)) == true 
                && this.isAlphabet(text.charAt(high)) == true)
			{
				// Both char is Alphabet
				first = first + text.charAt(high);
				last = text.charAt(low) + last;
				low++;
				high--;
			}
			else
			{
				if (this.isAlphabet(text.charAt(low)) == false)
				{
					first = first + text.charAt(low);
					low++;
				}
				if (this.isAlphabet(text.charAt(high)) == false)
				{
					last = text.charAt(high) + last;
					high--;
				}
			}
		}
		first += last;
		// Display result
		process.stdout.write("\n After : " + (first) + " \n");
	}
}

function main()
{
	var task = new ReverseOperation();
	// Test A
	task.reverseAlphabet("(e)x>chan?gi@n!g");
	// Test B
	task.reverseAlphabet("[M&a-nage]");
}
main();

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
#    Python 3 Program for
#    Reverse a string without affecting special characters
class ReverseOperation :
	def isAlphabet(self, value) :
		if ((value >= 'a'
				and value <= 'z') or(value >= 'A'
				and value <= 'Z')) :
			return True
		
		return False
	
	def reverseAlphabet(self, text) :
		#  Get number of characters in text
		n = len(text)
		#  Before replace text
		print(" Before : ", text, end = "")
		low = 0
		high = n - 1
		first = ""
		last = ""
		while (low < high) :
			if (self.isAlphabet(text[low]) == True and
                self.isAlphabet(text[high]) == True) :
				#  Both char is Alphabet
				first = first + str(text[high])
				last = str(text[low]) + last
				low += 1
				high -= 1
			else :
				if (self.isAlphabet(text[low]) == False) :
					first = first + str(text[low])
					low += 1
				
				if (self.isAlphabet(text[high]) == False) :
					last = str(text[high]) + last
					high -= 1
				
			
		
		first += last
		#  Display result
		print("\n After : ", (first) ," ")
	

def main() :
	task = ReverseOperation()
	#  Test A
	task.reverseAlphabet("(e)x>chan?gi@n!g")
	#  Test B
	task.reverseAlphabet("[M&a-nage]")

if __name__ == "__main__": main()

Output

 Before :  (e)x>chan?gi@n!g
 After :  (g)n>igna?hc@x!e
 Before :  [M&a-nage]
 After :  [e&g-anaM]
#    Ruby Program for
#    Reverse a string without affecting special characters
class ReverseOperation 
	def isAlphabet(value) 
		if ((value >= 'a' && value <= 'z') || 
            (value >= 'A' && value <= 'Z')) 
			return true
		end

		return false
	end

	def reverseAlphabet(text) 
		#  Get number of characters in text
		n = text.length
		#  Before replace text
		print(" Before : ", text)
		low = 0
		high = n - 1
		first = ""
		last = ""
		while (low < high) 
			if (self.isAlphabet(text[low]) == true && 
                self.isAlphabet(text[high]) == true) 
				#  Both char is Alphabet
				first = first + text[high].to_s
				last = text[low].to_s + last
				low += 1
				high -= 1
			else
 
				if (self.isAlphabet(text[low]) == false) 
					first = first + text[low].to_s
					low += 1
				end

				if (self.isAlphabet(text[high]) == false) 
					last = text[high].to_s + last
					high -= 1
				end

			end

		end

		first += last
		#  Display result
		print("\n After : ", (first) ," \n")
	end

end

def main() 
	task = ReverseOperation.new()
	#  Test A
	task.reverseAlphabet("(e)x>chan?gi@n!g")
	#  Test B
	task.reverseAlphabet("[M&a-nage]")
end

main()

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e 
 Before : [M&a-nage]
 After : [e&g-anaM] 
import scala.collection.mutable._;
/*
    Scala Program for
    Reverse a string without affecting special characters
*/
class ReverseOperation()
{
	def isAlphabet(value: Char): Boolean = {
		if ((value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z'))
		{
			return true;
		}
		return false;
	}
	def reverseAlphabet(text: String): Unit = {
		// Get number of characters in text
		var n: Int = text.length();
		// Before replace text
		print(" Before : " + text);
		var low: Int = 0;
		var high: Int = n - 1;
		var first: String = "";
		var last: String = "";
		while (low < high)
		{
			if (isAlphabet(text.charAt(low)) == true 
                && isAlphabet(text.charAt(high)) == true)
			{
				// Both char is Alphabet
				first = first + text.charAt(high).toString();
				last = text.charAt(low).toString() + last;
				low += 1;
				high -= 1;
			}
			else
			{
				if (isAlphabet(text.charAt(low)) == false)
				{
					first = first + text.charAt(low).toString();
					low += 1;
				}
				if (isAlphabet(text.charAt(high)) == false)
				{
					last = text.charAt(high).toString() + last;
					high -= 1;
				}
			}
		}
		first += last;
		// Display result
		print("\n After : " + (first) + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: ReverseOperation = new ReverseOperation();
		// Test A
		task.reverseAlphabet("(e)x>chan?gi@n!g");
		// Test B
		task.reverseAlphabet("[M&a-nage]");
	}
}

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]
import Foundation;
/*
    Swift 4 Program for
    Reverse a string without affecting special characters
*/
class ReverseOperation
{
	func isAlphabet(_ value: Character) -> Bool
	{
		if ((value >= "a" && value <= "z") || 
            (value >= "A" && value <= "Z"))
		{
			return true;
		}
		return false;
	}
	func reverseAlphabet(_ data: String)
	{
      	let text = Array(data);
		// Get number of characters in text
		let n: Int = text.count;
		// Before replace text
		print(" Before : ", data, terminator: "");
		var low: Int = 0;
		var high: Int = n - 1;
		var first: String = "";
		var last: String = "";
		while (low < high)
		{
			if (self.isAlphabet(text[low]) == true && 
                self.isAlphabet(text[high]) == true)
			{
				// Both char is Alphabet
				first = first + String(text[high]);
				last = String(text[low]) + last;
				low += 1;
				high -= 1;
			}
			else
			{
				if (self.isAlphabet(text[low]) == false)
				{
					first = first + String(text[low]);
					low += 1;
				}
				if (self.isAlphabet(text[high]) == false)
				{
					last = String(text[high]) + last;
					high -= 1;
				}
			}
		}
		first += last;
		// Display result
		print("\n After : ", (first) ," ");
	}
}
func main()
{
	let task: ReverseOperation = ReverseOperation();
	// Test A
	task.reverseAlphabet("(e)x>chan?gi@n!g");
	// Test B
	task.reverseAlphabet("[M&a-nage]");
}
main();

Output

 Before :  (e)x>chan?gi@n!g
 After :  (g)n>igna?hc@x!e
 Before :  [M&a-nage]
 After :  [e&g-anaM]
/*
    Kotlin Program for
    Reverse a string without affecting special characters
*/
class ReverseOperation
{
	fun isAlphabet(value: Char): Boolean
	{
		if ((value >= 'a' && value <= 'z') || 
            (value >= 'A' && value <= 'Z'))
		{
			return true;
		}
		return false;
	}
	fun reverseAlphabet(text: String): Unit
	{
		// Get number of characters in text
		val n: Int = text.length;
		// Before replace text
		print(" Before : " + text);
		var low: Int = 0;
		var high: Int = n - 1;
		var first: String = "";
		var last: String = "";
		while (low < high)
		{
			if (this.isAlphabet(text.get(low)) == true 
                && this.isAlphabet(text.get(high)) == true)
			{
				// Both char is Alphabet
				first = first + text.get(high).toString();
				last = text.get(low).toString() + last;
				low += 1;
				high -= 1;
			}
			else
			{
				if (this.isAlphabet(text.get(low)) == false)
				{
					first = first + text.get(low).toString();
					low += 1;
				}
				if (this.isAlphabet(text.get(high)) == false)
				{
					last = text.get(high).toString() + last;
					high -= 1;
				}
			}
		}
		first += last;
		// Display result
		print("\n After : " + (first) + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: ReverseOperation = ReverseOperation();
	// Test A
	task.reverseAlphabet("(e)x>chan?gi@n!g");
	// Test B
	task.reverseAlphabet("[M&a-nage]");
}

Output

 Before : (e)x>chan?gi@n!g
 After : (g)n>igna?hc@x!e
 Before : [M&a-nage]
 After : [e&g-anaM]




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment