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
Time complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read