Reverse alternate k characters in a string

Here given code implementation process.

//C Program
//Reverse alternate k characters in a string
#include <stdio.h>

//Reverse the substrings between given location
void reverse(char * text, int start, int end)
{
	if (start == end)
	{
		return;
	}
	char temp;
	//Reverse the string element from start to end location
	for (start; start < end; start++, end--)
	{
		temp = text[start];
		text[start] = text[end];
		text[end] = temp;
	}
}
//This function is reversing alternate groups of k characters
void reverse_alternate_char(char * text, int k, int size)
{
	if (k <= 0 || size <= 0)
	{
		return;
	}
	printf("\n Given String : %s\n", text);
	for (int i = 0; i <= size - k; i += k)
	{
		reverse(text, i, i + (k - 1));
		// skip group of k characters
		i += k;
	}
	printf(" Alternate Characters : %d\n", k);
}
int main()
{
	//Define the character elements
	char text1[] = "123ABC567EFG";
	//Get the size
	int size = sizeof(text1) / sizeof(text1[0]) - 1;
	//Get result
	reverse_alternate_char(text1, 3, size);
	printf(" After Result : %s\n", text1);
	// Other cases
	char text2[] = "12345ABCDE678";
	size = sizeof(text2) / sizeof(text2[0]) - 1;
	reverse_alternate_char(text2, 5, size);
	printf(" After Result : %s\n", text2);
	char text3[] = "1234ABCD1234ABCDEF";
	size = sizeof(text3) / sizeof(text3[0]) - 1;
	reverse_alternate_char(text3, 4, size);
	printf(" After Result : %s\n", text3);
	char text4[] = "ABCDEFGHIJKLMO";
	size = sizeof(text4) / sizeof(text4[0]) - 1;
	reverse_alternate_char(text4, 2, size);
	printf(" After Result : %s\n", text4);
	return 0;
}

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
//Java Program 
//Reverse alternate k characters in a string
class MyString
{
	//Reverse the substrings between given location
	//Returns reversal form of substring of given location
	public String reverse(String text, int start, int end)
	{
		if (start == end)
		{
			return "";
		}
		String result = "";
		//Reverse the string element from start to end location
		for (int i = start; i <=end ; i++)
		{
			result = text.charAt(i)+result;
		}
		return result;
	}
	//This function is reversing alternate groups of k characters
	public String reverse_alternate_char(String text, int k)
	{
		int size = text.length();

		if (k <= 0 || size <= 0)
		{
			return text;
		}
		//loop controlling variable [i]
		int i = 0;
		//This is store the resultant result
		String result = "";

		//This is used to detect reversal location
      	int counter = 0;

		System.out.print("\n Given String : " + text + "\n");
		
		//Loop iterating string elements
		for (i = 0; i < size ; i++)
		{
           	//Condition which is check valid reverse k characters elements
            if(counter==0 && i <= size - k)
            {
				result += reverse(text, i, i + (k - 1));
                
                counter = k;
            	
            	//Skip group of k-1 characters
                i = i + (k - 1);
            }
          	else
            {
              counter--;
              result += text.charAt(i);
            }
		}
		System.out.print(" Alternate Characters : " + k + "\n");
		return result;
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		String text = "123ABC567EFG";
		text = obj.reverse_alternate_char(text, 3);
		System.out.print(" After Result : " + text + "\n");
		text = "12345ABCDE678";
		text = obj.reverse_alternate_char(text, 5);
		System.out.print(" After Result : " + text + "\n");
		text = "1234ABCD1234ABCDEF";
		text = obj.reverse_alternate_char(text, 4);
		System.out.print(" After Result : " + text + "\n");
		text = "ABCDEFGHIJKLMO";
		text = obj.reverse_alternate_char(text, 2);
		System.out.print(" After Result : " + text + "\n");
	}
}

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
//Include header file
#include <iostream>
#include<string.h>
using namespace std;

//C++ Program 
//Reverse alternate k characters in a string
class MyString
{
	public:
		//Reverse the substrings between given location
		//Returns reversal form of substring of given location
		string reverse(string text, int start, int end)
		{
			if (start == end)
			{
				return "";
			}
			string result = "";
			//Reverse the string element from start to end location
			for (int i = start; i <= end; i++)
			{
				result = text[i] + result;
			}
			return result;
		}
	//This function is reversing alternate groups of k characters
	string reverse_alternate_char(string text, int k)
	{
		int size = text.size();
		if (k <= 0 || size <= 0)
		{
			return text;
		}
		//loop controlling variable [i,j]
		int i = 0;
		int j = 0;
		//This is store the resultant result
		string result = "";
		//This is used to detect reversal location
		int counter = 0;
		cout << "\n Given String : " << text << "\n";
		//Loop iterating string elements
		for (i = 0; i < size; i++)
		{
			//Condition which is check valid reverse k characters elements
			if (counter == 0 && i <= size - k)
			{
				result += this->reverse(text, i, i + (k - 1));
				counter = k;
				//Skip group of k-1 characters
				i = i + (k - 1);
			}
			else
			{
				counter--;
				result += text[i];
			}
		}
		cout << " Alternate Characters : " << k << "\n";
		return result;
	}
};
int main()
{
	MyString obj = MyString();
	string text = "123ABC567EFG";
	text = obj.reverse_alternate_char(text, 3);
	cout << " After Result : " << text << "\n";
	text = "12345ABCDE678";
	text = obj.reverse_alternate_char(text, 5);
	cout << " After Result : " << text << "\n";
	text = "1234ABCD1234ABCDEF";
	text = obj.reverse_alternate_char(text, 4);
	cout << " After Result : " << text << "\n";
	text = "ABCDEFGHIJKLMO";
	text = obj.reverse_alternate_char(text, 2);
	cout << " After Result : " << text << "\n";
	return 0;
}

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
//Include namespace system
using System;

//C# Program 
//Reverse alternate k characters in a string
class MyString
{
	//Reverse the substrings between given location
	//Returns reversal form of substring of given location
	public String reverse(String text, int start, int end)
	{
		if (start == end)
		{
			return "";
		}
		String result = "";
		//Reverse the string element from start to end location
		for (int i = start; i <= end; i++)
		{
			result = text[i] + result;
		}
		return result;
	}
	//This function is reversing alternate groups of k characters
	public String reverse_alternate_char(String text, int k)
	{
		int size = text.Length;
		if (k <= 0 || size <= 0)
		{
			return text;
		}
		//loop controlling variable [i]
		int i = 0;
		//This is store the resultant result
		String result = "";
		//This is used to detect reversal location
		int counter = 0;
		Console.Write("\n Given String : " + text + "\n");
		//Loop iterating string elements
		for (i = 0; i < size; i++)
		{
			//Condition which is check valid reverse k characters elements
			if (counter == 0 && i <= size - k)
			{
				result += reverse(text, i, i + (k - 1));
				counter = k;
				//Skip group of k-1 characters
				i = i + (k - 1);
			}
			else
			{
				counter--;
				result += text[i];
			}
		}
		Console.Write(" Alternate Characters : " + k + "\n");
		return result;
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		String text = "123ABC567EFG";
		text = obj.reverse_alternate_char(text, 3);
		Console.Write(" After Result : " + text + "\n");
		text = "12345ABCDE678";
		text = obj.reverse_alternate_char(text, 5);
		Console.Write(" After Result : " + text + "\n");
		text = "1234ABCD1234ABCDEF";
		text = obj.reverse_alternate_char(text, 4);
		Console.Write(" After Result : " + text + "\n");
		text = "ABCDEFGHIJKLMO";
		text = obj.reverse_alternate_char(text, 2);
		Console.Write(" After Result : " + text + "\n");
	}
}

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
<?php
//Php Program 
//Reverse alternate k characters in a string
class MyString
{
	//Reverse the substrings between given location
	//Returns reversal form of substring of given location
	public	function reverse($text, $start, $end)
	{
		if ($start == $end)
		{
			return "";
		}
		$result = "";
		//Reverse the string element from start to end location
		for ($i = $start; $i <= $end; $i++)
		{
			$result = $text[$i] . $result;
		}
		return $result;
	}
	//This function is reversing alternate groups of k characters
	public	function reverse_alternate_char($text, $k)
	{
		$size = strlen($text);
		if ($k <= 0 || $size <= 0)
		{
			return $text;
		}
		//loop controlling variable [i]
		$i = 0;
		//This is store the resultant result
		$result = "";
		//This is used to detect reversal location
		$counter = 0;
		echo "\n Given String : ". $text ."\n";
		//Loop iterating string elements
		for ($i = 0; $i < $size; $i++)
		{
			//Condition which is check valid reverse k characters elements
			if ($counter == 0 && $i <= $size - $k)
			{
				$result .= $this->reverse($text, $i, $i + ($k - 1));
				$counter = $k;
				//Skip group of k-1 characters
				$i = $i + ($k - 1);
			}
			else
			{
				$counter--;
				$result .= $text[$i];
			}
		}
		echo " Alternate Characters : ". $k ."\n";
		return $result;
	}
}

function main()
{
	$obj = new MyString();
	$text = "123ABC567EFG";
	$text = $obj->reverse_alternate_char($text, 3);
	echo " After Result : ". $text ."\n";
	$text = "12345ABCDE678";
	$text = $obj->reverse_alternate_char($text, 5);
	echo " After Result : ". $text ."\n";
	$text = "1234ABCD1234ABCDEF";
	$text = $obj->reverse_alternate_char($text, 4);
	echo " After Result : ". $text ."\n";
	$text = "ABCDEFGHIJKLMO";
	$text = $obj->reverse_alternate_char($text, 2);
	echo " After Result : ". $text ."\n";
}
main();

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
//Node Js Program 
//Reverse alternate k characters in a string
class MyString
{
	//Reverse the substrings between given location
	//Returns reversal form of substring of given location
	reverse(text, start, end)
	{
		if (start == end)
		{
			return "";
		}
		var result = "";
		//Reverse the string element from start to end location
		for (var i = start; i <= end; i++)
		{
			result = text[i] + result;
		}
		return result;
	}
	//This function is reversing alternate groups of k characters
	reverse_alternate_char(text, k)
	{
		var size = text.length;
		if (k <= 0 || size <= 0)
		{
			return text;
		}
		//loop controlling variable [i]
		var i = 0;
		//This is store the resultant result
		var result = "";
		//This is used to detect reversal location
		var counter = 0;
		process.stdout.write("\n Given String : " + text + "\n");
		//Loop iterating string elements
		for (i = 0; i < size; i++)
		{
			//Condition which is check valid reverse k characters elements
			if (counter == 0 && i <= size - k)
			{
				result += this.reverse(text, i, i + (k - 1));
				counter = k;
				//Skip group of k-1 characters
				i = i + (k - 1);
			}
			else
			{
				counter--;
				result += text[i];
			}
		}
		process.stdout.write(" Alternate Characters : " + k + "\n");
		return result;
	}
}

function main()
{
	var obj = new MyString();
	var text = "123ABC567EFG";
	text = obj.reverse_alternate_char(text, 3);
	process.stdout.write(" After Result : " + text + "\n");
	text = "12345ABCDE678";
	text = obj.reverse_alternate_char(text, 5);
	process.stdout.write(" After Result : " + text + "\n");
	text = "1234ABCD1234ABCDEF";
	text = obj.reverse_alternate_char(text, 4);
	process.stdout.write(" After Result : " + text + "\n");
	text = "ABCDEFGHIJKLMO";
	text = obj.reverse_alternate_char(text, 2);
	process.stdout.write(" After Result : " + text + "\n");
}
main();

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
# Python 3 Program 
# Reverse alternate k characters in a string
class MyString :
	# Reverse the substrings between given location
	# Returns reversal form of substring of given location
	def reverse(self, text, start, end) :
		if (start == end) :
			return ""
		
		result = ""
		# Reverse the string element from start to end location
		i = start
		while (i <= end) :
			result = text[i] + result
			i += 1
		
		return result
	
	# This function is reversing alternate groups of k characters
	def reverse_alternate_char(self, text, k) :
		size = len(text)
		if (k <= 0 or size <= 0) :
			return text
		
		# loop controlling variable [i]
		i = 0
		# This is store the resultant result
		result = ""
		# This is used to detect reversal location
		counter = 0
		print("\n Given String : ", text )
		# Loop iterating string elements
		while (i < size) :
			# Condition which is check valid reverse k characters elements
			if (counter == 0 and i <= size - k) :
				result += self.reverse(text, i, i + (k - 1))
				counter = k
				# Skip group of k-1 characters
				i = i + (k - 1)
			else :
				counter -= 1
				result += text[i]
			
			i += 1
		
		print(" Alternate Characters : ", k )
		return result
	

def main() :
	obj = MyString()
	text = "123ABC567EFG"
	text = obj.reverse_alternate_char(text, 3)
	print(" After Result : ", text )
	text = "12345ABCDE678"
	text = obj.reverse_alternate_char(text, 5)
	print(" After Result : ", text )
	text = "1234ABCD1234ABCDEF"
	text = obj.reverse_alternate_char(text, 4)
	print(" After Result : ", text )
	text = "ABCDEFGHIJKLMO"
	text = obj.reverse_alternate_char(text, 2)
	print(" After Result : ", text )

if __name__ == "__main__": main()

Output

 Given String :  123ABC567EFG
 Alternate Characters :  3
 After Result :  321ABC765EFG

 Given String :  12345ABCDE678
 Alternate Characters :  5
 After Result :  54321ABCDE678

 Given String :  1234ABCD1234ABCDEF
 Alternate Characters :  4
 After Result :  4321ABCD4321ABCDEF

 Given String :  ABCDEFGHIJKLMO
 Alternate Characters :  2
 After Result :  BACDFEGHJIKLOM
# Ruby Program 
# Reverse alternate k characters in a string
class MyString

	# Reverse the substrings between given location
	# Returns reversal form of substring of given location
	def reverse(text, start, ends)
	
		if (start == ends)
		
			return ""
		end
		result = ""
		# Reverse the string element from start to end location
		i = start
		while (i <= ends)
		
			result = text[i] + result
			i += 1
		end
		return result
	end
	# This function is reversing alternate groups of k characters
	def reverse_alternate_char(text, k)
	
		size = text.length()
		if (k <= 0 || size <= 0)
		
			return text
		end
		# loop controlling variable [i]
		i = 0
		# This is store the resultant result
		result = ""
		# This is used to detect reversal location
		counter = 0
		print("\n Given String : ", text ,"\n")
		# Loop iterating string elements
		while (i < size)
		
			# Condition which is check valid reverse k characters elements
			if (counter == 0 && i <= size - k)
			
				result += self.reverse(text, i, i + (k - 1))
				counter = k
				# Skip group of k-1 characters
				i = i + (k - 1)
			else
			
				counter -= 1
				result += text[i]
			end
			i += 1
		end
		print(" Alternate Characters : ", k ,"\n")
		return result
	end
end
def main()

	obj = MyString.new()
	text = "123ABC567EFG"
	text = obj.reverse_alternate_char(text, 3)
	print(" After Result : ", text ,"\n")
	text = "12345ABCDE678"
	text = obj.reverse_alternate_char(text, 5)
	print(" After Result : ", text ,"\n")
	text = "1234ABCD1234ABCDEF"
	text = obj.reverse_alternate_char(text, 4)
	print(" After Result : ", text ,"\n")
	text = "ABCDEFGHIJKLMO"
	text = obj.reverse_alternate_char(text, 2)
	print(" After Result : ", text ,"\n")
end
main()

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
//Scala Program 
//Reverse alternate k characters in a string
class MyString
{
	//Reverse the substrings between given location
	//Returns reversal form of substring of given location
	def reverse(text: String, start: Int, end: Int): String = {
		if (start == end)
		{
			return "";
		}
		var result: String = "";
		//Reverse the string element from start to end location
		var i: Int = start;
		while (i <= end)
		{
			result = ""+ text(i) + result;
			i += 1;
		}
		return result;
	}
	//This function is reversing alternate groups of k characters
	def reverse_alternate_char(text: String, k: Int): String = {
		var size: Int = text.length();
		if (k <= 0 || size <= 0)
		{
			return text;
		}
		//loop controlling variable [i]
		var i: Int = 0;
		//This is store the resultant result
		var result: String = "";
		//This is used to detect reversal location
		var counter: Int = 0;
		print("\n Given String : " + text + "\n");
		//Loop iterating string elements
		while (i < size)
		{
			//Condition which is check valid reverse k characters elements
			if (counter == 0 && i <= size - k)
			{
				result += reverse(text, i, i + (k - 1));
				counter = k;
				//Skip group of k-1 characters
				i = i + (k - 1);
			}
			else
			{
				counter -= 1;
				result += text(i);
			}
			i += 1;
		}
		print(" Alternate Characters : " + k + "\n");
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "123ABC567EFG";
		text = obj.reverse_alternate_char(text, 3);
		print(" After Result : " + text + "\n");
		text = "12345ABCDE678";
		text = obj.reverse_alternate_char(text, 5);
		print(" After Result : " + text + "\n");
		text = "1234ABCD1234ABCDEF";
		text = obj.reverse_alternate_char(text, 4);
		print(" After Result : " + text + "\n");
		text = "ABCDEFGHIJKLMO";
		text = obj.reverse_alternate_char(text, 2);
		print(" After Result : " + text + "\n");
	}
}

Output

 Given String : 123ABC567EFG
 Alternate Characters : 3
 After Result : 321ABC765EFG

 Given String : 12345ABCDE678
 Alternate Characters : 5
 After Result : 54321ABCDE678

 Given String : 1234ABCD1234ABCDEF
 Alternate Characters : 4
 After Result : 4321ABCD4321ABCDEF

 Given String : ABCDEFGHIJKLMO
 Alternate Characters : 2
 After Result : BACDFEGHJIKLOM
//Swift Program 
//Reverse alternate k characters in a string
class MyString
{
	//Reverse the substrings between given location
	//Returns reversal form of substring of given location
	func reverse(_ data: String, _ start: Int, _ end: Int) -> String
	{
      	var text = Array(data);
      
		if (start == end)
		{
			return "";
		}
		var result: String = "";
		//Reverse the string element from start to end location
		var i: Int = start;
		while (i <= end)
		{
			result = String(text[i]) + result;
			i += 1;
		}
		return result;
	}
	//This function is reversing alternate groups of k characters
	func reverse_alternate_char(_ data: String, _ k: Int) -> String
	{
		let size: Int = data.count;
      	let text = Array(data);
		if (k <= 0 || size <= 0)
		{
			return data;
		}
		//loop controlling variable [i]
		var i: Int = 0;
		//This is store the resultant result
		var result: String = "";
		//This is used to detect reversal location
		var counter: Int = 0;
		print("\n Given String : ", data );
		//Loop iterating string elements
		while (i < size)
		{
			//Condition which is check valid reverse k characters elements
			if (counter == 0 && i <= size - k)
			{
				result += self.reverse(data, i, i + (k - 1));
				counter = k;
				//Skip group of k-1 characters
				i = i + (k - 1);
			}
			else
			{
				counter -= 1;
				result += String(text[i]);
			}
			i += 1;
		}
		print(" Alternate Characters : ", k );
		return result;
	}
}
func main()
{
	let obj: MyString = MyString();
	var text: String = "123ABC567EFG";
	text = obj.reverse_alternate_char(text, 3);
	print(" After Result : ", text );
	text = "12345ABCDE678";
	text = obj.reverse_alternate_char(text, 5);
	print(" After Result : ", text );
	text = "1234ABCD1234ABCDEF";
	text = obj.reverse_alternate_char(text, 4);
	print(" After Result : ", text );
	text = "ABCDEFGHIJKLMO";
	text = obj.reverse_alternate_char(text, 2);
	print(" After Result : ", text );
}
main();

Output

 Given String :  123ABC567EFG
 Alternate Characters :  3
 After Result :  321ABC765EFG

 Given String :  12345ABCDE678
 Alternate Characters :  5
 After Result :  54321ABCDE678

 Given String :  1234ABCD1234ABCDEF
 Alternate Characters :  4
 After Result :  4321ABCD4321ABCDEF

 Given String :  ABCDEFGHIJKLMO
 Alternate Characters :  2
 After Result :  BACDFEGHJIKLOM


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







© 2021, kalkicode.com, All rights reserved