Transform string character to opposite case

Here given code implementation process.

// C Program
// Transform string character to opposite case
#include <stdio.h>

void transform_string(char str[],int size)
{
  printf("%s\n",str );
  
  for (int i = 0; i < size; ++i)
  {
    if(str[i] >= 'a' && str[i] <= 'z')
    {
      // When gets a lowercase character
      str[i]= str[i]-32;
    }
    else if(str[i] >= 'A' && str[i] <= 'Z')
    {
      // When gets a uppercase character
      str[i]= str[i]+32;
    }
  }
}
int main()
{

  char str1[]="This Is DiffiCult code";
  int size = sizeof(str1)/sizeof(str1[0])-1;
  
  transform_string(str1,size);
  printf("%s\n",str1 );

  char str2[]="you ARE programmer ?";
  size = sizeof(str2)/sizeof(str2[0]);

  transform_string(str2,size);
  printf("%s\n",str2 );
  return 0;
}

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
// Java program
// Transform string character to opposite case

public class MyString {

  public String transform_string(String str) {
    
    int size = str.length();
    
    System.out.print(str+"\n");

    String result ="";

    for (int i = 0; i < size; ++i) 
    {
      if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') 
      {
        // When gets a lowercase character
        result += (char)(str.charAt(i) - 32);
      } 
      else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') 
      {
        // When gets a uppercase character
        result +=  (char)(str.charAt(i) + 32);
      }
      else
      {
          result += str.charAt(i);
      }
    }
    return result;
  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();
    
    String text = "This Is DiffiCult code";

    text =   obj.transform_string(text);
    
    System.out.print(text+"\n");

    text = "you ARE programmer ?" ;

    text =  obj.transform_string(text);
    
    System.out.print(text+"\n");
  }
}

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
// C++ program
// Transform string character to opposite case
#include<iostream>

using namespace std;
class MyString {
	public:
		string transform_string(string str) {
			int size = str.size();
			cout << str << "\n";
			string result = "";
			for (int i = 0; i < size; ++i) {
				if (str[i] >= 'a' &&
					str[i] <= 'z') {
					// When gets a lowercase character
					result += (char)(str[i] - 32);
				} else
				if (str[i] >= 'A' &&
					str[i] <= 'Z') {
					// When gets a uppercase character
					result += (char)(str[i] + 32);
				} else {
					result += str[i];
				}
			}
			return result;
		}
};
int main() {
	MyString obj =  MyString();
	string text = "This Is DiffiCult code";
	text = obj.transform_string(text);
	cout << text << "\n";
	text = "you ARE programmer ?";
	text = obj.transform_string(text);
	cout << text << "\n";
	return 0;
}

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
// C# program
// Transform string character to opposite case
using System;
public class MyString {
	public String transform_string(String str) {
		int size = str.Length;
		Console.Write(str + "\n");
		String result = "";
		for (int i = 0; i < size; ++i) {
			if (str[i] >= 'a' &&
				str[i] <= 'z') {
				// When gets a lowercase character
				result += (char)(str[i] - 32);
			} else
			if (str[i] >= 'A' &&
				str[i] <= 'Z') {
				// When gets a uppercase character
				result += (char)(str[i] + 32);
			} else {
				result += str[i];
			}
		}
		return result;
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		String text = "This Is DiffiCult code";
		text = obj.transform_string(text);
		Console.Write(text + "\n");
		text = "you ARE programmer ?";
		text = obj.transform_string(text);
		Console.Write(text + "\n");
	}
}

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
<?php
// Php program
// Transform string character to opposite case
class MyString {
	public 	function transform_string($str) {
		$size = strlen($str);
		echo($str ."\n");
		$result = "";
		for ($i = 0; $i < $size; ++$i) {
			if (ord($str[$i]) >= ord('a') &&
				ord($str[$i]) <= ord('z')) {
				// When gets a lowercase character
				$result .= chr((ord($str[$i]) - 32));
			} 
          	else if (ord($str[$i]) >= ord('A') &&
				ord($str[$i]) <= ord('Z')) {
				// When gets a uppercase character
				$result .= chr((ord($str[$i]) + 32));
			} else {
				$result .= $str[$i];
			}
		}
		return $result;
	}
}

function main() {
	$obj = new MyString();
	$text = "This Is DiffiCult code";
	$text = $obj->transform_string($text);
	echo($text ."\n");
	$text = "you ARE programmer ?";
	$text = $obj->transform_string($text);
	echo($text ."\n");

}
main();

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
// Node Js program
// Transform string character to opposite case
class MyString {
	transform_string(str) {
		var size = str.length;
		process.stdout.write(str + "\n");
		var result = "";
		for (var i = 0; i < size; ++i) {
			if ((str[i]).charCodeAt(0) >= ('a').charCodeAt(0) &&
				(str[i]).charCodeAt(0) <= ('z').charCodeAt(0)) {
				// When gets a lowercase character
				result += String.fromCharCode(((str[i]).charCodeAt(0) - 32));
			} else
			if ((str[i]).charCodeAt(0) >= ('A').charCodeAt(0) &&
				(str[i]).charCodeAt(0) <= ('Z').charCodeAt(0)) {
				// When gets a uppercase character
				result += String.fromCharCode(((str[i]).charCodeAt(0) + 32));
			} else {
				result += str[i];
			}
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	var text = "This Is DiffiCult code";
	text = obj.transform_string(text);
	process.stdout.write(text + "\n");
	text = "you ARE programmer ?";
	text = obj.transform_string(text);
	process.stdout.write(text + "\n");
}

main();

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
#  Python 3 program
#  Transform string character to opposite case
class MyString :
	def transform_string(self, str) :
		size = len(str)
		print(str ,"\n", end = "")
		result = ""
		i = 0
		while (i < size) :
			if (ord(str[i]) >= ord('a') and ord(str[i]) <= ord('z')) :
				#  When gets a lowercase character
				result += chr((ord(str[i]) - 32))
			elif (ord(str[i]) >= ord('A') and ord(str[i]) <= ord('Z')) :
				#  When gets a uppercase character
				result += chr((ord(str[i]) + 32))
			else :
				result += str[i]
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "This Is DiffiCult code"
	text = obj.transform_string(text)
	print(text ,"\n", end = "")
	text = "you ARE programmer ?"
	text = obj.transform_string(text)
	print(text ,"\n", end = "")


if __name__ == "__main__":
	main()

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
#  Ruby program
#  Transform string character to opposite case
class MyString 
	def transform_string(str) 
		size = str.length()
		print(str ,"\n")
		result = ""
		i = 0
		while (i < size) 
			if ((str[i]).ord >= ('a').ord &&
				(str[i]).ord <= ('z').ord) 
				#  When gets a lowercase character
				result += ((str[i]).ord - 32).chr
			elsif ((str[i]).ord >= ('A').ord &&
				(str[i]).ord <= ('Z').ord) 
				#  When gets a uppercase character
				result += (str[i].ord + 32).chr
			else 
				result += str[i]
			end
			i += 1
		end
		return result
	end
end
def main() 
	obj = MyString.new()
	text = "This Is DiffiCult code"
	text = obj.transform_string(text)
	print(text ,"\n")
	text = "you ARE programmer ?"
	text = obj.transform_string(text)
	print(text ,"\n")
end
main()

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer ?
YOU are PROGRAMMER ?
// Scala program
// Transform string character to opposite case
class MyString {
	def transform_string(str: String): String = {
		var size: Int = str.length();
		print(str + "\n");
		var result: String = "";
		var i: Int = 0;
		while (i < size) {
			if (str(i) >= 'a' &&
				str(i) <= 'z') {
				// When gets a lowercase character
				result += ((str(i) - 32)).toChar;
			} else
			if (str(i) >= 'A' &&
				str(i) <= 'Z') {
				// When gets a uppercase character
				result += ((str(i) + 32)).toChar;
			} else {
				result += str(i);
			}
			i += 1;
		}
		return result;
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "This Is DiffiCult code";
		text = obj.transform_string(text);
		print(text + "\n");
		text = "you ARE programmer?";
		text = obj.transform_string(text);
		print(text + "\n");
	}
}

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer?
YOU are PROGRAMMER?
// Swift program
// Transform string character to opposite case
class MyString {
	func transform_string(_ str: String) -> String {
		let size: Int = str.count;
      	print(str);
		var result: String = "";
		var i: Int = 0;
		var data = Array(str);
      	var ch : String = " ";
		while (i < size) {
          	ch = String(data[i]);
			if (data[i] >= "a" &&
				data[i] <= "z") {
				// When gets a lowercase character
				result += String(UnicodeScalar(UInt8(UnicodeScalar(ch)!.value  - 32)));
			} else
			if (data[i] >= "A" &&
				data[i] <= "Z") {
				// When gets a uppercase character
				result += String(UnicodeScalar(UInt8(UnicodeScalar(ch)!.value  + 32)));
			} else {
				result += ch;
			}
			i += 1;
		}
		return result;
	}
}
func main() {
	let obj: MyString = MyString();
	var text: String = "This Is DiffiCult code";
	text = obj.transform_string(text);
	print(text ,"\n", terminator: "");
	text = "you ARE programmer?";
	text = obj.transform_string(text);
	print(text ,"\n", terminator: "");
}
main();

Output

This Is DiffiCult code
tHIS iS dIFFIcULT CODE
you ARE programmer?
YOU are PROGRAMMER?


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