C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q. Program to replace lower-case characters with upper-case and vice versa.
Here, our task is to replace all the lower-case characters in the string to upper-case and upper-case characters to lower-case. For this purpose, we need to traverse the string and check for each character. If the character is a lower-case character, make it upper-case by using the language-specific built-in method or add 32 to the lower-case character in C to change the ASCII value of the character. Algorithm
SolutionPython
str1="Great Power";
newStr = "";
for i in range(0, len(str1)):
#Checks for lower case character
if str1[i].islower():
#Convert it into upper case using upper () function
newStr += str1[i].upper();
#Checks for upper case character
elif str1[i].isupper():
#Convert it into lower case using lower () function
newStr += str1[i].lower();
else:
newStr += str1[i];
print("String after case conversion : " + newStr);
Output: String after case conversion : gREAT pOWER C
#include <stdio.h>
int main()
{
int i, len = 0;
char str[] = "Great Power";
//Calculating length of the array
len = sizeof(str)/sizeof(str[0]);
//Checks every character in the array
for(i = 0; i < len; i++){
//Checks whether a character is upper case character
if(isupper(str[i])){
//Convert that charcter to lower case
str[i] = tolower(str[i]);
}
//Checks whether a character is lower case character
else if(islower(str[i])){
//Convert that charcter to upper case
str[i] = toupper(str[i]);
}
}
printf("String after case conversion : %s", str);
return 0;
}
Output: String after case conversion : gREAT pOWER JAVA
public class changeCase {
public static void main(String[] args) {
String str1="Great Power";
StringBuffer newStr=new StringBuffer(str1);
for(int i = 0; i < str1.length(); i++) {
//Checks for lower case character
if(Character.isLowerCase(str1.charAt(i))) {
//Convert it into upper case using toUpperCase() function
newStr.setCharAt(i, Character.toUpperCase(str1.charAt(i)));
}
//Checks for upper case character
else if(Character.isUpperCase(str1.charAt(i))) {
//Convert it into upper case using toLowerCase() function
newStr.setCharAt(i, Character.toLowerCase(str1.charAt(i)));
}
}
System.out.println("String after case conversion : " + newStr);
}
}
Output: String after case conversion : gREAT pOWER C#
using System;
public class Program
{
public static void Main()
{
string str1="Great Power";
char ch;
System.Text.StringBuilder str2 = new System.Text.StringBuilder();
for(int i = 0; i < str1.Length; i++) {
//Checks for lower case character
if(char.IsLower(str1[i])) {
//Convert it into upper case using ToUpper() function
ch = Char.ToUpper(str1[i]);
//Append that character to new character
str2.Append(ch);
}
//Checks for lower case character
else if(char.IsUpper(str1[i])) {
//Convert it into lower case using ToLower() function
ch = Char.ToLower(str1[i]);
//Append that character to new character
str2.Append(ch);
}
else{
ch = str1[i];
str2.Append(ch);
}
}
Console.WriteLine("String after case conversion : " + str2);
}
}
Output: String after case conversion : gREAT pOWER PHP
<!DOCTYPE html>
<html>
<body>
<?php
$str1 = "Great Power";
for($i = 0; $i < strlen($str1); $i++){
//Checks for lower case character
if($str1[$i] >= 'a' && $str1[$i] <= 'z'){
//Convert it into upper case using strtoupper() function
$str1[$i] = strtoupper($str1[$i]);
}
//Checks for lower case character
elseif($str1[$i] >= 'A' && $str1[$i] <= 'Z'){
//Convert it into upper case using strtolower() function
$str1[$i] = strtolower($str1[$i]);
}
}
echo "String after case conversion : $str1";
?>
</body>
</html>
Output: String after case conversion : gREAT pOWER
Next TopicPrograms List
|