Check if a given string is a cyclic palindrome

Here given code implementation process.

/*
    C program for
    Check if a given string is a cyclic palindrome
*/
#include <stdio.h>
#include <string.h>

// This function is checking rotation palindrome of given indexes
int isPalindrome(char *text, int start, int n)
{
	// Left side 
	int left = start - 1;
	// Right side
	int right = start;
	int count = 0;
	while (count < n)
	{
		if (left < 0)
		{
			// When left is less than zero
			// Set last position
			left = n - 1;
		}
		if (right >= n)
		{
			// When right is greater than or equal to n
			// Set first position
			right = 0;
		}
		if (text[left] != text[right])
		{
			// When palindrome not occur
			return 0;
		}
		// Change counter value
		left--;
		right++;
		count++;
	}
	return 1;
}
void isCyclicPalindrome(char *text)
{
	// Get the number of text element
	int n = strlen(text);
	printf("\n Given Text : %s", text);
	for (int i = 0; i < n; ++i)
	{
		if (isPalindrome(text, i, n))
		{
			// When cyclic palindrome exist
			printf("\n Is cyclic palindrome\n");
			return;
		}
	}
	// When if rotational or cyclic palindrome not exist
	printf("\n Is not cyclic palindrome\n");
}
int main(int argc, char
	const *argv[])
{
	// xxxxyy =>  yxxxxy
	isCyclicPalindrome("xxxxyy");
	// abccba => abccba or cbaabc ...
	isCyclicPalindrome("abccba");
	isCyclicPalindrome("abccic");
	// abaaaaa => aaabaaa 
	isCyclicPalindrome("abaaaaa");
	return 0;
}

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
/*
    Java program for
    Check if a given string is a cyclic palindrome
*/
public class Palindrome
{
	// This function is checking rotation palindrome of given indexes
	public boolean isPalindrome(String text, int start, int n)
	{
		// Left side 
		int left = start - 1;
		// Right side
		int right = start;
		int count = 0;
		while (count < n)
		{
			if (left < 0)
			{
				// When left is less than zero
				// Set last position
				left = n - 1;
			}
			if (right >= n)
			{
				// When right is greater than or equal to n
				// Set first position
				right = 0;
			}
			if (text.charAt(left) != text.charAt(right))
			{
				// When palindrome not occur
				return false;
			}
			// Change counter value
			left--;
			right++;
			count++;
		}
		return true;
	}
	public void isCyclicPalindrome(String text)
	{
		// Get the number of text element
		int n = text.length();
		System.out.print("\n Given Text : " + text);
		for (int i = 0; i < n; ++i)
		{
			if (isPalindrome(text, i, n))
			{
				// When cyclic palindrome exist
				System.out.print("\n Is cyclic palindrome\n");
				return;
			}
		}
		// When if rotational or cyclic palindrome not exist
		System.out.print("\n Is not cyclic palindrome\n");
	}
	public static void main(String[] args)
	{
		Palindrome task = new Palindrome();
		// xxxxyy =>  yxxxxy
		task.isCyclicPalindrome("xxxxyy");
		// abccba => abccba or cbaabc ...
		task.isCyclicPalindrome("abccba");
		task.isCyclicPalindrome("abccic");
		// abaaaaa => aaabaaa 
		task.isCyclicPalindrome("abaaaaa");
	}
}

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
    C++ program for
    Check if a given string is a cyclic palindrome
*/
class Palindrome
{
	public:
		// This function is checking rotation palindrome of given indexes
		bool isPalindrome(string text, int start, int n)
		{
			// Left side 
			int left = start - 1;
			// Right side
			int right = start;
			int count = 0;
			while (count < n)
			{
				if (left < 0)
				{
					// When left is less than zero
					// Set last position
					left = n - 1;
				}
				if (right >= n)
				{
					// When right is greater than or equal to n
					// Set first position
					right = 0;
				}
				if (text[left] != text[right])
				{
					// When palindrome not occur
					return false;
				}
				// Change counter value
				left--;
				right++;
				count++;
			}
			return true;
		}
	void isCyclicPalindrome(string text)
	{
		// Get the number of text element
		int n = text.length();
		cout << "\n Given Text : " << text;
		for (int i = 0; i < n; ++i)
		{
			if (this->isPalindrome(text, i, n))
			{
				// When cyclic palindrome exist
				cout << "\n Is cyclic palindrome\n";
				return;
			}
		}
		// When if rotational or cyclic palindrome not exist
		cout << "\n Is not cyclic palindrome\n";
	}
};
int main()
{
	Palindrome *task = new Palindrome();
	// xxxxyy =>  yxxxxy
	task->isCyclicPalindrome("xxxxyy");
	// abccba => abccba or cbaabc ...
	task->isCyclicPalindrome("abccba");
	task->isCyclicPalindrome("abccic");
	// abaaaaa => aaabaaa 
	task->isCyclicPalindrome("abaaaaa");
	return 0;
}

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
// Include namespace system
using System;
/*
    Csharp program for
    Check if a given string is a cyclic palindrome
*/
public class Palindrome
{
	// This function is checking rotation palindrome of given indexes
	public Boolean isPalindrome(String text, int start, int n)
	{
		// Left side 
		int left = start - 1;
		// Right side
		int right = start;
		int count = 0;
		while (count < n)
		{
			if (left < 0)
			{
				// When left is less than zero
				// Set last position
				left = n - 1;
			}
			if (right >= n)
			{
				// When right is greater than or equal to n
				// Set first position
				right = 0;
			}
			if (text[left] != text[right])
			{
				// When palindrome not occur
				return false;
			}
			// Change counter value
			left--;
			right++;
			count++;
		}
		return true;
	}
	public void isCyclicPalindrome(String text)
	{
		// Get the number of text element
		int n = text.Length;
		Console.Write("\n Given Text : " + text);
		for (int i = 0; i < n; ++i)
		{
			if (this.isPalindrome(text, i, n))
			{
				// When cyclic palindrome exist
				Console.Write("\n Is cyclic palindrome\n");
				return;
			}
		}
		// When if rotational or cyclic palindrome not exist
		Console.Write("\n Is not cyclic palindrome\n");
	}
	public static void Main(String[] args)
	{
		Palindrome task = new Palindrome();
		// xxxxyy =>  yxxxxy
		task.isCyclicPalindrome("xxxxyy");
		// abccba => abccba or cbaabc ...
		task.isCyclicPalindrome("abccba");
		task.isCyclicPalindrome("abccic");
		// abaaaaa => aaabaaa 
		task.isCyclicPalindrome("abaaaaa");
	}
}

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
<?php
/*
    Php program for
    Check if a given string is a cyclic palindrome
*/
class Palindrome
{
	// This function is checking rotation palindrome of given indexes
	public	function isPalindrome($text, $start, $n)
	{
		// Left side 
		$left = $start - 1;
		// Right side
		$right = $start;
		$count = 0;
		while ($count < $n)
		{
			if ($left < 0)
			{
				// When left is less than zero
				// Set last position
				$left = $n - 1;
			}
			if ($right >= $n)
			{
				// When right is greater than or equal to n
				// Set first position
				$right = 0;
			}
			if ($text[$left] != $text[$right])
			{
				// When palindrome not occur
				return false;
			}
			// Change counter value
			$left--;
			$right++;
			$count++;
		}
		return true;
	}
	public	function isCyclicPalindrome($text)
	{
		// Get the number of text element
		$n = strlen($text);
		echo("\n Given Text : ".$text);
		for ($i = 0; $i < $n; ++$i)
		{
			if ($this->isPalindrome($text, $i, $n))
			{
				// When cyclic palindrome exist
				echo("\n Is cyclic palindrome\n");
				return;
			}
		}
		// When if rotational or cyclic palindrome not exist
		echo("\n Is not cyclic palindrome\n");
	}
}

function main()
{
	$task = new Palindrome();
	// xxxxyy =>  yxxxxy
	$task->isCyclicPalindrome("xxxxyy");
	// abccba => abccba or cbaabc ...
	$task->isCyclicPalindrome("abccba");
	$task->isCyclicPalindrome("abccic");
	// abaaaaa => aaabaaa 
	$task->isCyclicPalindrome("abaaaaa");
}
main();

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
/*
    Node JS program for
    Check if a given string is a cyclic palindrome
*/
class Palindrome
{
	// This function is checking rotation palindrome of given indexes
	isPalindrome(text, start, n)
	{
		// Left side 
		var left = start - 1;
		// Right side
		var right = start;
		var count = 0;
		while (count < n)
		{
			if (left < 0)
			{
				// When left is less than zero
				// Set last position
				left = n - 1;
			}
			if (right >= n)
			{
				// When right is greater than or equal to n
				// Set first position
				right = 0;
			}
			if (text.charAt(left) != text.charAt(right))
			{
				// When palindrome not occur
				return false;
			}
			// Change counter value
			left--;
			right++;
			count++;
		}
		return true;
	}
	isCyclicPalindrome(text)
	{
		// Get the number of text element
		var n = text.length;
		process.stdout.write("\n Given Text : " + text);
		for (var i = 0; i < n; ++i)
		{
			if (this.isPalindrome(text, i, n))
			{
				// When cyclic palindrome exist
				process.stdout.write("\n Is cyclic palindrome\n");
				return;
			}
		}
		// When if rotational or cyclic palindrome not exist
		process.stdout.write("\n Is not cyclic palindrome\n");
	}
}

function main()
{
	var task = new Palindrome();
	// xxxxyy =>  yxxxxy
	task.isCyclicPalindrome("xxxxyy");
	// abccba => abccba or cbaabc ...
	task.isCyclicPalindrome("abccba");
	task.isCyclicPalindrome("abccic");
	// abaaaaa => aaabaaa 
	task.isCyclicPalindrome("abaaaaa");
}
main();

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
#    Python 3 program for
#    Check if a given string is a cyclic palindrome
class Palindrome :
	#  This function is checking rotation palindrome of given indexes
	def isPalindrome(self, text, start, n) :
		#  Left side 
		left = start - 1
		#  Right side
		right = start
		count = 0
		while (count < n) :
			if (left < 0) :
				#  When left is less than zero
				#  Set last position
				left = n - 1
			
			if (right >= n) :
				#  When right is greater than or equal to n
				#  Set first position
				right = 0
			
			if (text[left] != text[right]) :
				#  When palindrome not occur
				return False
			
			#  Change counter value
			left -= 1
			right += 1
			count += 1
		
		return True
	
	def isCyclicPalindrome(self, text) :
		#  Get the number of text element
		n = len(text)
		print("\n Given Text : ", text, end = "")
		i = 0
		while (i < n) :
			if (self.isPalindrome(text, i, n)) :
				#  When cyclic palindrome exist
				print("\n Is cyclic palindrome")
				return
			
			i += 1
		
		#  When if rotational or cyclic palindrome not exist
		print("\n Is not cyclic palindrome")
	

def main() :
	task = Palindrome()
	#  xxxxyy =>  yxxxxy
	task.isCyclicPalindrome("xxxxyy")
	#  abccba => abccba or cbaabc ...
	task.isCyclicPalindrome("abccba")
	task.isCyclicPalindrome("abccic")
	#  abaaaaa => aaabaaa 
	task.isCyclicPalindrome("abaaaaa")

if __name__ == "__main__": main()

Output

 Given Text :  xxxxyy
 Is cyclic palindrome

 Given Text :  abccba
 Is cyclic palindrome

 Given Text :  abccic
 Is not cyclic palindrome

 Given Text :  abaaaaa
 Is cyclic palindrome
#    Ruby program for
#    Check if a given string is a cyclic palindrome
class Palindrome 
	#  This function is checking rotation palindrome of given indexes
	def isPalindrome(text, start, n) 
		#  Left side 
		left = start - 1
		#  Right side
		right = start
		count = 0
		while (count < n) 
			if (left < 0) 
				#  When left is less than zero
				#  Set last position
				left = n - 1
			end

			if (right >= n) 
				#  When right is greater than or equal to n
				#  Set first position
				right = 0
			end

			if (text[left] != text[right]) 
				#  When palindrome not occur
				return false
			end

			#  Change counter value
			left -= 1
			right += 1
			count += 1
		end

		return true
	end

	def isCyclicPalindrome(text) 
		#  Get the number of text element
		n = text.length
		print("\n Given Text : ", text)
		i = 0
		while (i < n) 
			if (self.isPalindrome(text, i, n)) 
				#  When cyclic palindrome exist
				print("\n Is cyclic palindrome\n")
				return
			end

			i += 1
		end

		#  When if rotational or cyclic palindrome not exist
		print("\n Is not cyclic palindrome\n")
	end

end

def main() 
	task = Palindrome.new()
	#  xxxxyy =>  yxxxxy
	task.isCyclicPalindrome("xxxxyy")
	#  abccba => abccba or cbaabc ...
	task.isCyclicPalindrome("abccba")
	task.isCyclicPalindrome("abccic")
	#  abaaaaa => aaabaaa 
	task.isCyclicPalindrome("abaaaaa")
end

main()

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
/*
    Scala program for
    Check if a given string is a cyclic palindrome
*/
class Palindrome()
{
	// This function is checking rotation palindrome of given indexes
	def isPalindrome(text: String, start: Int, n: Int): Boolean = {
		// Left side 
		var left: Int = start - 1;
		// Right side
		var right: Int = start;
		var count: Int = 0;
		while (count < n)
		{
			if (left < 0)
			{
				// When left is less than zero
				// Set last position
				left = n - 1;
			}
			if (right >= n)
			{
				// When right is greater than or equal to n
				// Set first position
				right = 0;
			}
			if (text.charAt(left) != text.charAt(right))
			{
				// When palindrome not occur
				return false;
			}
			// Change counter value
			left -= 1;
			right += 1;
			count += 1;
		}
		return true;
	}
	def isCyclicPalindrome(text: String): Unit = {
		// Get the number of text element
		var n: Int = text.length();
		print("\n Given Text : " + text);
		var i: Int = 0;
		while (i < n)
		{
			if (isPalindrome(text, i, n))
			{
				// When cyclic palindrome exist
				print("\n Is cyclic palindrome\n");
				return;
			}
			i += 1;
		}
		// When if rotational or cyclic palindrome not exist
		print("\n Is not cyclic palindrome\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Palindrome = new Palindrome();
		// xxxxyy =>  yxxxxy
		task.isCyclicPalindrome("xxxxyy");
		// abccba => abccba or cbaabc ...
		task.isCyclicPalindrome("abccba");
		task.isCyclicPalindrome("abccic");
		// abaaaaa => aaabaaa 
		task.isCyclicPalindrome("abaaaaa");
	}
}

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
import Foundation;
/*
    Swift 4 program for
    Check if a given string is a cyclic palindrome
*/
class Palindrome
{
	// This function is checking rotation palindrome of given indexes
	func isPalindrome(_ arr: [Character], _ start: Int, _ n: Int) -> Bool
	{
		// Left side 
		var left: Int = start - 1;
		// Right side
		var right: Int = start;
		var count: Int = 0;
		while (count < n)
		{
			if (left < 0)
			{
				// When left is less than zero
				// Set last position
				left = n - 1;
			}
			if (right >= n)
			{
				// When right is greater than or equal to n
				// Set first position
				right = 0;
			}
			if (arr[left]  != arr[right])
			{
				// When palindrome not occur
				return false;
			}
			// Change counter value
			left -= 1;
			right += 1;
			count += 1;
		}
		return true;
	}
	func isCyclicPalindrome(_ text: String)
	{
		// Get the number of text element
		let n: Int = text.count;
		print("\n Given Text : ", text, terminator: "");
		var i: Int = 0;
		let arr = Array(text);
		while (i < n)
		{
			if (self.isPalindrome(arr, i, n))
			{
				// When cyclic palindrome exist
				print("\n Is cyclic palindrome");
				return;
			}
			i += 1;
		}
		// When if rotational or cyclic palindrome not exist
		print("\n Is not cyclic palindrome");
	}
}
func main()
{
	let task: Palindrome = Palindrome();
	// xxxxyy =>  yxxxxy
	task.isCyclicPalindrome("xxxxyy");
	// abccba => abccba or cbaabc ...
	task.isCyclicPalindrome("abccba");
	task.isCyclicPalindrome("abccic");
	// abaaaaa => aaabaaa 
	task.isCyclicPalindrome("abaaaaa");
}
main();

Output

 Given Text :  xxxxyy
 Is cyclic palindrome

 Given Text :  abccba
 Is cyclic palindrome

 Given Text :  abccic
 Is not cyclic palindrome

 Given Text :  abaaaaa
 Is cyclic palindrome
/*
    Kotlin program for
    Check if a given string is a cyclic palindrome
*/
class Palindrome
{
	// This function is checking rotation palindrome of given indexes
	fun isPalindrome(text: String, start: Int, n: Int): Boolean
	{
		// Left side 
		var left: Int = start - 1;
		// Right side
		var right: Int = start;
		var count: Int = 0;
		while (count < n)
		{
			if (left < 0)
			{
				// When left is less than zero
				// Set last position
				left = n - 1;
			}
			if (right >= n)
			{
				// When right is greater than or equal to n
				// Set first position
				right = 0;
			}
			if (text.get(left) != text.get(right))
			{
				// When palindrome not occur
				return false;
			}
			// Change counter value
			left -= 1;
			right += 1;
			count += 1;
		}
		return true;
	}
	fun isCyclicPalindrome(text: String): Unit
	{
		// Get the number of text element
		val n: Int = text.length;
		print("\n Given Text : " + text);
		var i: Int = 0;
		while (i < n)
		{
			if (this.isPalindrome(text, i, n))
			{
				// When cyclic palindrome exist
				print("\n Is cyclic palindrome\n");
				return;
			}
			i += 1;
		}
		// When if rotational or cyclic palindrome not exist
		print("\n Is not cyclic palindrome\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Palindrome = Palindrome();
	// xxxxyy =>  yxxxxy
	task.isCyclicPalindrome("xxxxyy");
	// abccba => abccba or cbaabc ...
	task.isCyclicPalindrome("abccba");
	task.isCyclicPalindrome("abccic");
	// abaaaaa => aaabaaa 
	task.isCyclicPalindrome("abaaaaa");
}

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome
package main
import "fmt"
/*
    Go program for
    Check if a given string is a cyclic palindrome
*/

// This function is checking rotation palindrome of given indexes
func isPalindrome(text string, start int, n int) bool {
	// Left side 
	var left int = start - 1
	// Right side
	var right int = start
	var count int = 0
	for (count < n) {
		if left < 0 {
			// When left is less than zero
			// Set last position
			left = n - 1
		}
		if right >= n {
			// When right is greater than or equal to n
			// Set first position
			right = 0
		}
		if text[left] != text[right] {
			// When palindrome not occur
			return false
		}
		// Change counter value
		left--
		right++
		count++
	}
	return true
}
func isCyclicPalindrome(text string) {
	// Get the number of text element
	var n int = len(text)
	fmt.Print("\n Given Text : ", text)
	for i := 0 ; i < n ; i++ {
		if isPalindrome(text, i, n) {
			// When cyclic palindrome exist
			fmt.Print("\n Is cyclic palindrome\n")
			return
		}
	}
	// When if rotational or cyclic palindrome not exist
	fmt.Print("\n Is not cyclic palindrome\n")
}
func main() {

	// xxxxyy =>  yxxxxy
	isCyclicPalindrome("xxxxyy")
	// abccba => abccba or cbaabc ...
	isCyclicPalindrome("abccba")
	isCyclicPalindrome("abccic")
	// abaaaaa => aaabaaa 
	isCyclicPalindrome("abaaaaa")
}

Output

 Given Text : xxxxyy
 Is cyclic palindrome

 Given Text : abccba
 Is cyclic palindrome

 Given Text : abccic
 Is not cyclic palindrome

 Given Text : abaaaaa
 Is cyclic palindrome


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