Remove vowels from a string: Difference between revisions
m (→using character eliding: changed a comment.) |
(Added Go) |
||
Line 2: | Line 2: | ||
Remove vowels from a string |
Remove vowels from a string |
||
=={{header|Go}}== |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"strings" |
|||
) |
|||
func removeVowels(s string) string { |
|||
var sb strings.Builder |
|||
vowels := "aeiouAEIOU" |
|||
for _, c := range s { |
|||
if !strings.ContainsAny(string(c), vowels) { |
|||
sb.WriteRune(c) |
|||
} |
|||
} |
|||
return sb.String() |
|||
} |
|||
func main() { |
|||
s := "Go Programming Language" |
|||
fmt.Println("Input :", s) |
|||
fmt.Println("Output :", removeVowels(s)) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Input : Go Programming Language |
|||
Output : G Prgrmmng Lngg |
|||
</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
Revision as of 10:04, 25 July 2020
Remove vowels from a string
Go
<lang go>package main
import (
"fmt" "strings"
)
func removeVowels(s string) string {
var sb strings.Builder vowels := "aeiouAEIOU" for _, c := range s { if !strings.ContainsAny(string(c), vowels) { sb.WriteRune(c) } } return sb.String()
}
func main() {
s := "Go Programming Language" fmt.Println("Input :", s) fmt.Println("Output :", removeVowels(s))
}</lang>
- Output:
Input : Go Programming Language Output : G Prgrmmng Lngg
REXX
using the TRANSLATE BIF
<lang rexx>/*REXX program removes vowels (both lowercase and uppercase) from a string. */ parse arg x /*obtain optional argument from the CL.*/ if x= | x="," then x= 'REXX Programming Language' /*Not specified? Then use default*/ say ' input string: ' x $= translate( xrange(), ., ' ') /*define a string of almost all chars. */ q= substr($, verify($, x), 1) /*find a character NOT in the X string.*/ y= translate(x, q, " ") /*trans. blanks in the string (for now)*/ y= space(translate(y, , 'AEIOUaeiou'), 0) /*trans. vowels──►blanks, elide blanks.*/ y= translate(y, , q) /*trans the Q characters back to blanks*/ say 'output string: ' y /*stick a fork in it, we're all done. */</lang>
- output when using the default input:
input string: REXX Programming Language output string: RXX Prgrmmng Lngg
using character eliding
<lang rexx>/*REXX program removes vowels (both lowercase and uppercase) from a string. */ parse arg x /*obtain optional argument from the CL.*/ if x= | x="," then x= 'REXX Programming Language' /*Not specified? Then use default*/ say ' input string: ' x x= . || x /*prefix string with a dummy character.*/
do j=length(x)-1 by -1 for length(x)-1 /*process the string from the back─end.*/ _= pos( substr(x, j, 1), 'AEIOUaeiou') /*is this particular character a vowel?*/ if _==0 then iterate /*if zero (not a vowel), then skip it.*/ x= left(x, j - 1) || substr(x, j + 1) /*elide the vowel just detected from X.*/ end /*j*/
x= substr(x, 2) /*elide the prefixed dummy character. */ say 'output string: ' x /*stick a fork in it, we're all done. */</lang>
- output is identical to the 1st REXX version.
Ring
<lang ring> load "stdlib.ring" str = "Ring Programming Language" see "Input : " + str + nl for n = 1 to len(str)
if isVowel(str[n]) str = substr(str,str[n],"") ok
next see "String without vowels: " + str + nl </lang>
- Output:
Input : Ring Programming Language String without vowels: Rng Prgrmmng Lngg