Program Discussion :: Strings
10 / 60
Write a program to convert the String to its toggle form.
Answer:
import java.util.Scanner;
public class CaseManipulation {
public static String toToggleCase(String inputString) {
String result = "";
for (int i = 0; i < inputString.length(); i ) {
char currentChar = inputString.charAt(i);
if (Character.isUpperCase(currentChar)) {
char currentCharToLowerCase = Character.toLowerCase(currentChar);
result = result currentCharToLowerCase;
} else {
char currentCharToUpperCase = Character.toUpperCase(currentChar);
result = result currentCharToUpperCase;
}
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an input String: ");
String inputString = scanner.nextLine();
System.out.println("Toggle Case: " toToggleCase(inputString));
}
}
Asked In ::
Language:

Rai
8 May, 2017 9:13 PM
import java.util.Scanner;
public class CaseManipulation {
public static String toToggleCase(String inputString) {
String result = "";
for (int i = 0; i < inputString.length(); i ) {
char currentChar = inputString.charAt(i);
if (Character.isUpperCase(currentChar)) {
char currentCharToLowerCase = Character.toLowerCase(currentChar);
result = result currentCharToLowerCase;
} else {
char currentCharToUpperCase = Character.toUpperCase(currentChar);
result = result currentCharToUpperCase;
}
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an input String: ");
String inputString = scanner.nextLine();
System.out.println("Toggle Case: " toToggleCase(inputString));
}
}
Language:

Aditya
1 Jul, 2017 10:19 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Toggle(){
$str ="HEllo"; /* input string */
echo "Input string is :" . $str."<br/>";
$var = str_split($str); /* conversion of string into array */
$arrlength=count ($var); /* counting lengh of array */
$arr=array();
for($i=0; $i< $arrlength; $i++)
{
$arr[$i]= ord($var[$i]);
}
for($x=0; $x< $arrlength; $x++) /* loop for reading the input array */
{
if($arr[$x]>=65 && $arr[$x]<=92)
{
$arr[$x]=$arr[$x]+32;
}
else
{
$arr[$x]=$arr[$x]-32;
}
}
for($j=0; $j< $arrlength; $j++)
{
$var[$j]= chr($arr[$j]);
}
$name= implode ("",$var); /* conversion of array into string */
echo $name; /* printing string without vowels */
}
Toggle();
?>
</body>
</html>
Language:

Rahul
7 Jul, 2017 9:30 AM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[20];
int i;
printf("Enter the String (Enter First Name) in uppercase : ");
scanf("%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65 && str[i]<=92)
{
str[i]=str[i]+32;
}
else{
str[i]=str[i]-32;
}
}
printf("\nThe String in Lowercase = %s",str);
getch();
}
Language:

Neha
7 Jul, 2017 9:30 AM
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[20];
int i;
str= " WhAT iS YouR nAme?";
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65 && str[i]<=92)
{
str[i]=str[i]+32;
}
else{
str[i]=str[i]-32;
}
}
cout<<"\nThe String in = "<<str;
getch();
}
Language:

Veera Chowdary Kamani
7 Sep, 2018 11:46 AM
string=input("enter your string\n")
print(string.swapcase())
Language:

Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.io.*;
public class ToggleCase
{
public static void main(String []args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int x,y=0;
String str;
System.out.print("Enter a string : ");
str=br.readLine();
System.out.print("\nIn Toggle case : ");
for(int i=0;i<str.length();i++)
{
x=(int)str.charAt(i);
if(x!=' ')
{
if(x>=65 && x<=90)
y=x+32;
if(x>=97 && x<=122)
y=x-32;
System.out.print((char)y);
}
else
System.out.print(" ");
}
}
}