Toggle alphabet characters in a string

Here given code implementation process.

/*
    C program for
    Toggle alphabet characters in a string
*/
#include <stdio.h>
#include <string.h>

void toggleAlphabet(char *text)
{
	// Get number of characters in text
	int n = strlen(text);
	// Before replace text
	printf("\nBefore : %s", text);
	for (int i = 0; i < n; ++i)
	{
		if ((text[i] >= 'a' && text[i] <= 'z') || 
            (text[i] >= 'A' && text[i] <= 'Z'))
			// Replace to other case
			text[i] = text[i] ^ 32;
	}
	// After replace text
	printf("\n After : %s", text);
}
int main(int argc, char
	const *argv[])
{
	char text1[] = "-gOOGLE2021-";
	char text2[] = "SiMPle";
	// Test case
	toggleAlphabet(text1);
	toggleAlphabet(text2);
	return 0;
}

Output

Before : -gOOGLE2021-
 After : -Google2021-
Before : SiMPle
 After : sImpLE
/*
    Java Program for
    Toggle alphabet characters in a string
*/
public class Toggle
{
    public void toggleAlphabet(String text)
    {
        // Get number of characters in text
        int n = text.length();
        // Before replace text
        System.out.print("\nBefore : " + text);
        String result = "";
        for (int i = 0; i < n; ++i)
        {
            if ((text.charAt(i) >= 'a' && text.charAt(i) <= 'z') || 
                (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z'))
            {
                // Replace to other case
                result += (char)(text.charAt(i) ^ 32);
            }
            else
            {
                result += text.charAt(i);
            }
        }
        // Display calculated result
        System.out.print("\n Result : " + result);
    }
    public static void main(String[] args)
    {
        Toggle task = new Toggle();
        // Test case
        task.toggleAlphabet("-gOOGLE2021-");
        task.toggleAlphabet("SiMPle");
    }
}

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
// Include header file
#include <iostream>

#include <string>

using namespace std;
/*
    C++ Program for
    Toggle alphabet characters in a string
*/
class Toggle
{
    public: void toggleAlphabet(string text)
    {
        // Get number of characters in text
        int n = text.length();
        // Before replace text
        cout << "\nBefore : " << text;
    
        for (int i = 0; i < n; ++i)
        {
            if ((text[i] >= 'a' && text[i] <= 'z') || 
                (text[i] >= 'A' && text[i] <= 'Z'))
            {
                // Replace to other case
                text[i] =  (char)(text[i] ^ 32);
            }
            
        }
        // Display calculated result
        cout << "\n Result : " << text;
    }
};
int main()
{
    Toggle *task = new Toggle();
    // Test case
    task->toggleAlphabet("-gOOGLE2021-");
    task->toggleAlphabet("SiMPle");
    return 0;
}

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
// Include namespace system
using System;
/*
    Csharp Program for
    Toggle alphabet characters in a string
*/
public class Toggle
{
	public void toggleAlphabet(String text)
	{
		// Get number of characters in text
		int n = text.Length;
		// Before replace text
		Console.Write("\nBefore : " + text);
		String result = "";
		for (int i = 0; i < n; ++i)
		{
			if ((text[i] >= 'a' && text[i] <= 'z') || 
                (text[i] >= 'A' && text[i] <= 'Z'))
			{
				// Replace to other case
				result += (char)(text[i] ^ 32);
			}
			else
			{
				result += text[i];
			}
		}
		// Display calculated result
		Console.Write("\n Result : " + result);
	}
	public static void Main(String[] args)
	{
		Toggle task = new Toggle();
		// Test case
		task.toggleAlphabet("-gOOGLE2021-");
		task.toggleAlphabet("SiMPle");
	}
}

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
package main
import "fmt"

/*
    Go Program for
    Toggle alphabet characters in a string
*/
type Toggle struct {}
func getToggle() * Toggle {
	var me *Toggle = &Toggle {}
	return me
}
func(this Toggle) toggleAlphabet(text string) {
	// Get number of characters in text
	var n int = len(text)
	// Before replace text
	fmt.Print("\nBefore : ", text)
	var result string = ""
	for i := 0 ; i < n ; i++ {
		if (text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z') {
			// Replace to other case
			result += string(int(text[i]) ^ 32)
		} else {
			result += string(text[i])
		}
	}
	// Display calculated result
	fmt.Print("\n Result : ", result)
}
func main() {
	var task * Toggle = getToggle()
	// Test case
	task.toggleAlphabet("-gOOGLE2021-")
	task.toggleAlphabet("SiMPle")
}

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
<?php
/*
    Php Program for
    Toggle alphabet characters in a string
*/
class Toggle
{
	public	function toggleAlphabet($text)
	{
		// Get number of characters in text
		$n = strlen($text);
		// Before replace text
		echo("\nBefore : ".$text);
		$result = "";
		for ($i = 0; $i < $n; ++$i)
		{
			if (($text[$i] >= 'a' && $text[$i] <= 'z') || 
                ($text[$i] >= 'A' && $text[$i] <= 'Z'))
			{
				// Replace to other case
				$result .= chr((ord($text[$i]) ^ 32));
			}
			else
			{
				$result .= $text[$i];
			}
		}
		// Display calculated result
		echo("\n Result : ".$result);
	}
}

function main()
{
	$task = new Toggle();
	// Test case
	$task->toggleAlphabet("-gOOGLE2021-");
	$task->toggleAlphabet("SiMPle");
}
main();

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
/*
    Node JS Program for
    Toggle alphabet characters in a string
*/
class Toggle
{
	toggleAlphabet(text)
	{
		// Get number of characters in text
		var n = text.length;
		// Before replace text
		process.stdout.write("\nBefore : " + text);
		var result = "";
		for (var i = 0; i < n; ++i)
		{
			if ((text.charAt(i) >= 'a' && text.charAt(i) <= 'z') ||
                (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z'))
			{
				// Replace to other case
				result += String.fromCharCode(
                  (text.charAt(i).charCodeAt(0) ^ 32)
                );
			}
			else
			{
				result += text.charAt(i);
			}
		}
		// Display calculated result
		process.stdout.write("\n Result : " + result);
	}
}

function main()
{
	var task = new Toggle();
	// Test case
	task.toggleAlphabet("-gOOGLE2021-");
	task.toggleAlphabet("SiMPle");
}
main();

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
#    Python 3 Program for
#    Toggle alphabet characters in a string
class Toggle :
	def toggleAlphabet(self, text) :
		#  Get number of characters in text
		n = len(text)
		#  Before replace text
		print("\nBefore : ", text, end = "")
		result = ""
		i = 0
		while (i < n) :
			if ((text[i] >= 'a'
					and text[i] <= 'z') or (text[i] >= 'A'
					and text[i] <= 'Z')) :
				#  Replace to other case
				result += chr((ord(text[i]) ^ 32))
			else :
				result += text[i]
			
			i += 1
		
		#  Display calculated result
		print("\n Result : ", result, end = "")
	

def main() :
	task = Toggle()
	#  Test case
	task.toggleAlphabet("-gOOGLE2021-")
	task.toggleAlphabet("SiMPle")

if __name__ == "__main__": main()

Output

Before :  -gOOGLE2021-
 Result :  -Google2021-
Before :  SiMPle
 Result :  sImpLE
#    Ruby Program for
#    Toggle alphabet characters in a string
class Toggle 
	def toggleAlphabet(text) 
		#  Get number of characters in text
		n = text.length
		#  Before replace text
		print("\nBefore : ", text)
		result = ""
		i = 0
		while (i < n) 
			if ((text[i] >= 'a' && text[i] <= 'z') || 
                (text[i] >= 'A' && text[i] <= 'Z')) 
				#  Replace to other case
				result += ((text[i].ord ^ 32)).chr
			else
 
				result += text[i]
			end

			i += 1
		end

		#  Display calculated result
		print("\n Result : ", result)
	end

end

def main() 
	task = Toggle.new()
	#  Test case
	task.toggleAlphabet("-gOOGLE2021-")
	task.toggleAlphabet("SiMPle")
end

main()

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
import scala.collection.mutable._;
/*
    Scala Program for
    Toggle alphabet characters in a string
*/
class Toggle()
{
	def toggleAlphabet(text: String): Unit = {
		// Get number of characters in text
		var n: Int = text.length();
		// Before replace text
		print("\nBefore : " + text);
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			if ((text.charAt(i) >= 'a' && text.charAt(i) <= 'z') 
                || (text.charAt(i) >= 'A' && text.charAt(i) <= 'Z'))
			{
				// Replace to other case
				result += (text.charAt(i).toInt ^ 32).toChar;
			}
			else
			{
				result += text.charAt(i);
			}
			i += 1;
		}
		// Display calculated result
		print("\n Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Toggle = new Toggle();
		// Test case
		task.toggleAlphabet("-gOOGLE2021-");
		task.toggleAlphabet("SiMPle");
	}
}

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE
import Foundation;
/*
    Swift 4 Program for
    Toggle alphabet characters in a string
*/
class Toggle
{
	func toggleAlphabet(_ data: String)
	{
        let text = Array(data);
		// Get number of characters in text
		let n: Int = text.count;
		// Before replace text
		print("\nBefore : ", data, terminator: "");
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			if ((text[i] >= "a" && text[i] <= "z") || (text[i] >= "A" && text[i] <= "Z"))
			{
				// Replace to other case
				result +=
              String(
                  UnicodeScalar(
                    UInt8(UnicodeScalar(String(text[i]))!.value) ^ 32
                  )
             );
			}
			else
			{
				result += String(text[i]);
			}
			i += 1;
		}
		// Display calculated result
		print("\n Result : ", result, terminator: "");
	}
}
func main()
{
	let task: Toggle = Toggle();
	// Test case
	task.toggleAlphabet("-gOOGLE2021-");
	task.toggleAlphabet("SiMPle");
}
main();

Output

Before :  -gOOGLE2021-
 Result :  -Google2021-
Before :  SiMPle
 Result :  sImpLE
/*
    Kotlin Program for
    Toggle alphabet characters in a string
*/
class Toggle
{
	fun toggleAlphabet(text: String): Unit
	{
		// Get number of characters in text
		val n: Int = text.length;
		// Before replace text
		print("\nBefore : " + text);
		var result: String = "";
		var i: Int = 0;
		while (i < n)
		{
			if ((text.get(i) >= 'a' && text.get(i) <= 'z') || 
                (text.get(i) >= 'A' && text.get(i) <= 'Z'))
			{
				// Replace to other case
				result += (text.get(i).toInt() xor 32).toChar();
			}
			else
			{
				result += text.get(i);
			}
			i += 1;
		}
		// Display calculated result
		print("\n Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Toggle = Toggle();
	// Test case
	task.toggleAlphabet("-gOOGLE2021-");
	task.toggleAlphabet("SiMPle");
}

Output

Before : -gOOGLE2021-
 Result : -Google2021-
Before : SiMPle
 Result : sImpLE

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