Transform the string
Given a string s, change the string s according to the rules provided below:
- Delete all the vowels from the string.
- Insert # in front of all the consonants.
- Change the case of all the letters of the string.
Examples:
Input : aBAcAba Output :#b#C#B Input :SunshinE!! Output :#s#N#S#H#N!!
Approach: First, create a new string by removing all the vowels from the given original string. Now, change the case of every alphabet in this string. Finally, add # in front of every alphabet in the string and this is required string.
Implementation:
C++
// CPP code to transform string #include <bits/stdc++.h> using namespace std; // Function to change // character's case string change_case(string a) { int l = a.length(); for ( int i = 0 ; i < l ; i++) { // If character is lowercase // change to uppercase if (a[i] >= 'a' && a[i] <= 'z' ) a[i] = a[i] - 32; // If character is uppercase // change to lowercase else if (a[i] >= 'A' && a[i] <= 'Z' ) a[i] = a[i] + 32; } return a; } // Function to delete vowels string delete_vowels(string a) { string temp = "" ; int l = a.length(); for ( int i = 0 ; i < l ; i++) { //If character is consonant if (a[i] != 'a' && a[i] != 'e' && a[i] != 'i' && a[i] != 'o' && a[i] != 'u' && a[i] != 'A' && a[i] != 'E' && a[i] != 'O' && a[i] != 'U' && a[i] != 'I' ) temp += a[i]; } return temp; } // Function to insert "#" string insert_hash(string a) { string temp = "" ; int l = a.length(); for ( int i = 0 ; i < l ; i++) { // If character is not special if ((a[i] >= 'a' && a[i] <= 'z' ) || (a[i] >= 'A' && a[i] <= 'Z' )) temp = temp + '#' + a[i]; else temp = temp + a[i]; } return temp; } // Function to transform string void transformSting(string a) { string b = delete_vowels(a); string c = change_case(b); string d = insert_hash(c); //corner case // when all the words of string are vowel then string empty after deletion if (d== "" ) cout<< "-1" <<endl; else cout << d<<endl; } // Driver function int main() { string a = "SunshinE!!" ; string b = "aeiou" ; // Calling function transformSting(a); transformSting(b); return 0; } |
Java
// Java code to transform string import java.io.*; class Gfg { // Function to change // character's case public static String change_case(String a) { String temp = "" ; int l = a.length(); for ( int i = 0 ; i < l ; i++) { char ch=a.charAt(i); // If character is lowercase // change to uppercase if (ch >= 'a' &&ch <= 'z' ) ch = ( char )( 65 + ( int )(ch - 'a' )); // If character is uppercase // change to lowercase else if (ch >= 'A' &&ch <= 'Z' ) ch = ( char )( 97 + ( int )(ch - 'A' )); temp += ch; } return temp; } // Function to delete vowels public static String delete_vowels(String a) { String temp = "" ; int l = a.length(); for ( int i = 0 ; i < l ; i++) { char ch = a.charAt(i); // If character is consonant if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u' && ch != 'A' && ch != 'E' && ch != 'O' && ch != 'U' &&ch != 'I' ) temp += ch; } return temp; } // Function to insert "#" public static String insert_hash(String a) { String temp = "" ; int l = a.length(); char hash = '#' ; for ( int i = 0 ; i < l ; i++) { char ch=a.charAt(i); // If character is not // special character if ((ch >= 'a' && ch <= 'z' ) || (ch >= 'A' && ch <= 'Z' )) temp = temp + hash + ch; else temp = temp + ch; } return temp; } // Function to transform string public static void transformString(String a) { String b = delete_vowels(a); String c = change_case(b); String d = insert_hash(c); if (d== "" ) System.out.println( "-1" ); else System.out.println(d); } // Driver function public static void main (String args[]) { String a = "SunshinE!!" ; String b = "aeiou" ; // Calling function transformString(a); transformString(b); } } |
Python3
# Python code to # transform string # def to change # character's case def change_case(s) : a = list (s) l = len (s) for i in range ( 0 , l) : # If character is # lowercase change # to uppercase if (a[i] > = 'a' and a[i] < = 'z' ) : a[i] = s[i].upper() # If character is uppercase # change to lowercase elif (a[i] > = 'A' and a[i] < = 'Z' ) : a[i] = s[i].lower() return a # def to delete vowels def delete_vowels(s) : temp = "" a = list (s) l = len (s) for i in range ( 0 , l) : # If character # is consonant if (a[i] ! = 'a' and a[i] ! = 'e' and a[i] ! = 'i' and a[i] ! = 'o' and a[i] ! = 'u' and a[i] ! = 'A' and a[i] ! = 'E' and a[i] ! = 'O' and a[i] ! = 'U' and a[i] ! = 'I' ) : temp = temp + a[i] return temp # def to insert "#" def insert_hash(s) : temp = "" a = list (s) l = len (s) for i in range ( 0 , l) : # If character is # not special if ((a[i] > = 'a' and a[i] < = 'z' ) or (a[i] > = 'A' and a[i] < = 'Z' )) : temp = temp + '#' + a[i] else : temp = temp + a[i] return temp # def to # transform string def transformSting(a) : b = delete_vowels(a) c = change_case(b) d = insert_hash(c) print (d) # Driver Code a = "SunshinE!!" # Calling def transformSting(a) # This code is contributed by # Manish Shaw(manishshaw1) |
C#
// C# code to transform string using System; class Gfg { // Function to change // character's case public static String change_case( string a) { string temp = "" ; int l = a.Length; for ( int i = 0 ; i < l ; i++) { char ch=a[i]; // If character is lowercase // change to uppercase if (ch >= 'a' &&ch <= 'z' ) ch = ( char )(65 + ( int )(ch - 'a' )); // If character is uppercase // change to lowercase else if (ch >= 'A' &&ch <= 'Z' ) ch = ( char )(97 + ( int )(ch - 'A' )); temp += ch; } return temp; } // Function to delete vowels public static String delete_vowels(String a) { String temp = "" ; int l = a.Length; for ( int i = 0 ; i < l ; i++) { char ch = a[i]; // If character is consonant if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u' && ch != 'A' && ch != 'E' && ch != 'O' && ch != 'U' &&ch != 'I' ) temp += ch; } return temp; } // Function to insert "#" public static String insert_hash(String a) { String temp = "" ; int l = a.Length; char hash = '#' ; for ( int i = 0 ; i < l ; i++) { char ch=a[i]; // If character is not // special character if ((ch >= 'a' && ch <= 'z' ) || (ch >= 'A' && ch <= 'Z' )) temp = temp + hash + ch; else temp = temp + ch; } return temp; } // Function to transform string public static void transformString( string a) { string b = delete_vowels(a); string c = change_case(b); string d = insert_hash(c); Console.WriteLine(d); } // Driver function public static void Main () { string a = "SunshinE!!" ; // Calling function transformString(a); } } // This code is contributed by vt_m. |
PHP
<?php // PHP code to transform string // Function to change // character's case function change_case( $a ) { $l = strlen ( $a ); for ( $i = 0 ; $i < $l ; $i ++) { // If character is lowercase // change to uppercase if ( $a [ $i ] >= 'a' && $a [ $i ] <= 'z' ) $a [ $i ] = chr (65 + (ord( $a [ $i ]) - ord( 'a' ))); // If character is uppercase // change to lowercase else if ( $a [ $i ] >= 'A' && $a [ $i ] <= 'Z' ) $a [ $i ] = chr (97 + (ord( $a [ $i ]) - ord( 'a' ))); } return $a ; } // Function to delete vowels function delete_vowels( $a ) { $temp = "" ; $l = strlen ( $a ); for ( $i = 0 ; $i < $l ; $i ++) { // If character // is consonant if ( $a [ $i ] != 'a' && $a [ $i ] != 'e' && $a [ $i ] != 'i' && $a [ $i ] != 'o' && $a [ $i ] != 'u' && $a [ $i ] != 'A' && $a [ $i ] != 'E' && $a [ $i ] != 'O' && $a [ $i ] != 'U' && $a [ $i ] != 'I' ) $temp = $temp . $a [ $i ]; } return $temp ; } // Function to insert "#" function insert_hash( $a ) { $temp = "" ; $l = strlen ( $a ); for ( $i = 0 ; $i < $l ; $i ++) { // If character is // not special if (( $a [ $i ] >= 'a' && $a [ $i ] <= 'z' ) || ( $a [ $i ] >= 'A' && $a [ $i ] <= 'Z' )) $temp = $temp . '#' . $a [ $i ]; else $temp = $temp . $a [ $i ]; } return $temp ; } // Function to // transform string function transformSting( $a ) { $b = delete_vowels( $a ); $c = change_case( $b ); $d = insert_hash( $c ); echo ( $d ); } // Driver Code $a = "SunshinE!!" ; // Calling function transformSting( $a ); // This code is contributed by // Manish Shaw(manishshaw1) ?> |
Javascript
// Function to change // character's case function change_case(a) { var temp = "" ; var l = a.length; for ( var i=0; i < l; i++) { var ch = a.charAt(i); // If character is lowercase // change to uppercase if (ch >= 'a ' && ch <= ' z ') { ch = String.fromCharCode((65 + parseInt((ch.charCodeAt(0) - ' a '.charCodeAt(0))))); } else if (ch >= ' A ' && ch <= ' Z ') { ch = String.fromCharCode((97 + parseInt((ch.charCodeAt(0) - ' A '.charCodeAt(0))))); } temp += ch; } return temp; } // Function to delete vowels function delete_vowels(a) { var temp = ""; var l = a.length; for (var i=0; i < l; i++) { var ch = a.charAt(i); // If character is consonant if (ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' && ch != ' A ' && ch != ' E ' && ch != ' O ' && ch != ' U ' && ch != ' I ') { temp += ch; } } return temp; } // Function to insert "#" function insert_hash(a) { var temp = ""; var l = a.length; var hash = ' #'; for ( var i=0; i < l; i++) { var ch = a.charAt(i); // If character is not // special character if ((ch >= 'a ' && ch <= ' z ') || (ch >= ' A ' && ch <= ' Z')) { temp = temp + hash + ch; } else { temp = temp + ch; } } return temp; } // Function to transform string function transformString(a) { var b = delete_vowels(a); var c = change_case(b); var d = insert_hash(c); if (d== "" ) console.log( "-1" ); else console.log(d); } // Driver function var a = "SunshinE!!" ; var b = "aeiou" ; // Calling function transformString(a); transformString(b); // This code is contributed by Aarti_Rathi |
Output
#s#N#S#H#N!! -1
Time complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string
Please Login to comment...