Transform the string

30 Jun, 2022
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. 
C++
Java
Python3
C#
PHP
Javascript
// CPP code to transform string#include <bits/stdc++.h>using namespace std; // Function to change// character's casestring 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 vowelsstring 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 stringvoid 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 functionint main(){    string a = "SunshinE!!";    string b = "aeiou";    // Calling function    transformSting(a);    transformSting(b);    return 0;}
// Java code to transform stringimport 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);    }}
# Python code to# transform string # def to change# character's casedef 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 vowelsdef 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 stringdef transformSting(a) :     b = delete_vowels(a)    c = change_case(b)    d = insert_hash(c)     print (d) # Driver Codea = "SunshinE!!" # Calling deftransformSting(a) # This code is contributed by# Manish Shaw(manishshaw1)
// C# code to transform stringusing 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 code to transform string // Function to change// character's casefunction 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 vowelsfunction 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 stringfunction transformSting($a){    $b = delete_vowels($a);    $c = change_case($b);    $d = insert_hash($c);     echo ($d);} // Driver Code$a = "SunshinE!!"; // Calling functiontransformSting($a); // This code is contributed by// Manish Shaw(manishshaw1)?>
// Function to change// character's casefunction 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 vowelsfunction 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 stringfunction 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 functionvar a = "SunshinE!!";var b = "aeiou";// Calling functiontransformString(a);transformString(b); // This code is contributed by Aarti_Rathi
#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 
manishshaw1
abhishek0719kadiyan
surbhikumaridav
geekygirl2001
rohitmishra051000
codewithrathi
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Different Methods to Reverse a String in C++
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Longest Palindromic Substring | Set 1
Length of the longest substring without repeating characters
Top 50 String Coding Problems for Interviews
What is Data Structure: Types, Classifications and Applications
Convert string to char array in C++
Check whether two strings are anagram of each other