Remove vowels from a string

From Rosetta Code
Revision as of 12:19, 25 July 2020 by Thundergnat (talk | contribs) (→‎{{header|Raku}}: Probably should capture œ ligatures too)
Remove vowels from a string is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Remove vowels from a string

ALGOL 68

<lang algol68>BEGIN

   # returns s with the vowels removed #
   OP DEVOWEL = ( STRING s )STRING:
      BEGIN
           [ LWB s : UPB s ]CHAR result;
           INT r pos := LWB result - 1;
           FOR s pos FROM LWB s TO UPB s DO
               IF NOT char in string( s[ s pos ], NIL, "aeiouAEIOU" ) THEN
                   # have a non-vowel - add it to the output #
                   r pos +:= 1;
                   result[ r pos ] := s[ s pos ]
               FI
           OD;
           result[ LWB s : r pos ]
      END # DEVOWEL # ;
   # tests the DEVOWEL operator #
   PROC test devowel = ( STRING s )VOID:
        print( ( "<", s, "> -> <", DEVOWEL s, ">", newline ) );
   # some test cases #
   test devowel( ""                              );
   test devowel( "aAeEiIoOuU"                    );
   test devowel( "bcdfghjklmnprstvwxyz"          );
   test devowel( "abcdefghijklmnoprstuvwxyz"     );
   test devowel( "Algol 68 Programming Language" )

END</lang>

Output:
<> -> <>
<aAeEiIoOuU> -> <>
<bcdfghjklmnprstvwxyz> -> <bcdfghjklmnprstvwxyz>
<abcdefghijklmnoprstuvwxyz> -> <bcdfghjklmnprstvwxyz>
<Algol 68 Programming Language> -> <lgl 68 Prgrmmng Lngg>

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

Raku

Works with: Rakudo version 2020.07

Not going to bother with 'y', it's too touchy feely (How many vowels are in the word my? why? any?) and subject to interpretation.

Otherwise, this should work for most Latinate languages.

Spec is 'Remove vowels from a string'; nothing about what they should be replaced with. I chose to replace them with spaces.

Strings from http://mylanguages.org/. No affiliation, but it's a nice resource. (note: these are not all the same sentence but are all from the same paragraph. They frankly were picked based on their vowel load.)

<lang perl6>my @vowels = (0x20 .. 0x2fff).map: { .chr if .chr.samemark('x') ~~ m:i/<[aæeioœu]>/ }

my $text = q:to/END/;

  Undervisningen skal være gratis, i det minste på de elementære og grunnleggende trinn.
  Skal hún veitt ókeypis, að minnsta kosti barnafræðsla og undirstöðummentu.
  Hochschulunterricht muß allen gleichermaßen entsprechend ihren Fähigkeiten offenstehen.
  Öğrenim hiç olmazsa ilk ve temel safhalarında parasızdır. İlk öğretim mecburidir.
  L'éducation doit être gratuite, au moins en ce qui concerne l'enseignement élémentaire et fondamental.
  La instrucción elemental será obligatoria. La instrucción técnica y profesional habrá de ser generalizada.
  Education shall be free, at least in the elementary and fundamental stages.
  END

print my $lang = "Norwegian, Icelandic, German, Turkish, French, Spanish, English:\n"; put $text;

print ($lang, $text)».subst(/@vowels/, ' ', :g);</lang>

Output:
Norwegian, Icelandic, German, Turkish, French, Spanish, English:
Undervisningen skal være gratis, i det minste på de elementære og grunnleggende trinn.
Skal hún veitt ókeypis, að minnsta kosti barnafræðsla og undirstöðummentu.
Hochschulunterricht muß allen gleichermaßen entsprechend ihren Fähigkeiten offenstehen.
Öğrenim hiç olmazsa ilk ve temel safhalarında parasızdır. İlk öğretim mecburidir.
L'éducation doit être gratuite, au moins en ce qui concerne l'enseignement élémentaire et fondamental.
La instrucción elemental será obligatoria. La instrucción técnica y profesional habrá de ser generalizada.
Education shall be free, at least in the elementary and fundamental stages.

N rw g  n,  c l nd c, G rm n, T rk sh, Fr nch, Sp n sh,  ngl sh:
  nd rv sn ng n sk l v r  gr t s,   d t m nst  p  d   l m nt r   g gr nnl gg nd  tr nn.
Sk l h n v  tt  k yp s,  ð m nnst  k st  b rn fr ðsl   g  nd rst ð mm nt .
H chsch l nt rr cht m ß  ll n gl  ch rm ß n  ntspr ch nd  hr n F h gk  t n  ff nst h n.
 ğr n m h ç  lm zs   lk v  t m l s fh l rınd  p r sızdır.  lk  ğr t m m cb r d r.
L' d c t  n d  t  tr  gr t  t ,    m  ns  n c  q   c nc rn  l' ns  gn m nt  l m nt  r   t f nd m nt l.
L   nstr cc  n  l m nt l s r   bl g t r  . L   nstr cc  n t cn c  y pr f s  n l h br  d  s r g n r l z d .
 d c t  n sh ll b  fr  ,  t l  st  n th   l m nt ry  nd f nd m nt l st g s.

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

Wren

<lang ecmascript>var removeVowels = Fn.new { |s| s.where { |c| !"aeiouAEIOU".contains(c) }.join() }

var s = "Wren Programming Language" System.print("Input  : %(s)") System.print("Output : %(removeVowels.call(s))")</lang>

Output:
Input  : Wren Programming Language
Output : Wrn Prgrmmng Lngg