7
\$\begingroup\$

Challenge: Read a string from user, print number of each vowel, then place all remaining chars in a line. Shortest answer wins!

Output must look like this:

Enter String: Input

Vowel Count: a: #, e: #, i: #, o: #, u: #

Remaining characters: Vowel-less Input

For example:

Enter String: LoremIpsumDolorSitAmetConsecuteurAdipscingElit!
Vowel Count: a: 0, e: 4, i: 4, o: 4, u: 3
Remaining characters: LrmIpsmDlrStAmtCnsctrAdpscngElt!
\$\endgroup\$
  • 5
    \$\begingroup\$ Okay. I can certainly do that. And thank you, wonderful community, for being more forgiving than Stack Overflow to noobs. \$\endgroup\$ – Caleb Woodman May 3 '16 at 16:31
  • 6
    \$\begingroup\$ You're treating lowercase a different than capital A? \$\endgroup\$ – AdmBorkBork May 3 '16 at 16:45
  • 5
    \$\begingroup\$ Also, for future challenges, I heartily recommend using the Sandbox, so you can elicit feedback before posting to the main page, and thus make your challenge even better. Welcome to PPCG! \$\endgroup\$ – AdmBorkBork May 3 '16 at 16:46
  • 4
    \$\begingroup\$ The fun fact is that we are trying to be nice \$\endgroup\$ – edc65 May 3 '16 at 19:27
  • 5
    \$\begingroup\$ @CalebWoodman I don't think it has anything to do with being a noob or not -- rather, restrictive input/output requirements on non-Kolmogorov challenges shift the focus of the challenge, so that instead of actually doing work on the interesting part, people instead need to account for the I/O requirements, which instead feels like drudgery. Please don't take the downvotes personally (see the PostScript to Peter's answer)! \$\endgroup\$ – AdmBorkBork May 3 '16 at 19:30

19 Answers 19

5
\$\begingroup\$

05AB1E, 62 bytes

Code:

”‡Á•Ë: ÿ”,”Vowelš‹: ”?"aeiou"©vy": "¹y¢««}", "ý,“«¶šÞ: “ª¹®-J,

Explanation:

There is a lot of string compression going on here. Here is a list of all strings that are compressed:

To see how the actual string compression works, see the Hello, World! answer, or an even more elaborate explanation at the bottom of the Code Review question. Without the string compression, the code looks like this:

"Enter String: ÿ","Vowel Count: "?"aeiou"©vy": "¹y¢««}", "ý,"Remaining characters: "¹®-J,

Or a more readable 3-part version:

"Enter String: ÿ",
"Vowel Count: "?"aeiou"©vy": "¹y¢««}", "ý,
"Remaining characters: "¹®-J,

Part 1:

We first push the string "Enter String: ÿ". The ÿ here is used for string interpolation. But since the stack is empty, implicit input is requested. The ÿ will be substituted with the user input. We print this with a newline using ,.

Part 2:

We first print the "Vowel Count: " string without a newline using ?. After that, we push the aeiou string, ©opy that to the register and map over the string using v and do the following:

vy": "¹y¢««}    # Mapping loop

v          }    # The loop itself
 y              # Push the current letter (which is a, e, i, o or u)
  ": "          # Push this string
      ¹         # Push the first input again
       y        # Push the letter again
        ¢       # Count the number of occurences of that letter in the input
         ««     # Concatenate to one single string, (e.g. "a: 4")

This leaves 5 different string onto the stack, the stack looks like this now:

stack: ['a: 0', 'e: 1', 'i: 0', 'o: 2', 'u: 0']

We join each element in the stack with ", " using ý. After this, we print it with a newline using ,.

Part 3:

We first push the string "Remaining characters: ". We push the first input again using ¹. We then push "aeiou" which we retrieve from the register using ®. After this, we remove all letters in aeiou using - and Join this with the Remaining characters-string. Finally, we output this with a newline using ,.

Uses CP-1252 encoding. Try it online!.

\$\endgroup\$
  • 2
    \$\begingroup\$ I've seen answers with 05AB1E a couple of times now, but like most languages on this stackexchange I don't understand a lot of it.. Which of the characters in your code-snippet are used for the words "Enter", "String", "Count", "Remaining", and "Characters"? I can clearly see the "Vowelš" in the code (with an š instead of an s..), but I'm clueless about the others. (If possible I'd love a complete description of the entire code-snippet.) \$\endgroup\$ – Kevin Cruijssen May 4 '16 at 12:15
  • 2
    \$\begingroup\$ @KevinCruijssen I have added an explanation. If you have any other questions, feel free to ask them :). \$\endgroup\$ – Adnan May 4 '16 at 12:41
  • 1
    \$\begingroup\$ Wow, thanks for the clear and extended explanation. I know it's not just for me but also in general to others, but I really appreciate the afford explaining it in such a clear way. :) \$\endgroup\$ – Kevin Cruijssen May 4 '16 at 12:50
  • 1
    \$\begingroup\$ This is every sort of amazing! \$\endgroup\$ – Caleb Woodman May 7 '16 at 23:52
3
\$\begingroup\$

Python, 168 bytes

s=input('Enter String:')
v='aeiou'
print('Vowel Count: '+', '.join([a+':'+str(s.count(a)) for a in v])+'\nRemaining characters: '+''.join([a for a in s if a not in v]))

https://repl.it/CMzO/1

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to PPCG! Nice first post. \$\endgroup\$ – Rɪᴋᴇʀ May 4 '16 at 14:03
3
\$\begingroup\$

JavaScript (ES6), 165

Fixed error in output, thx @Neil
3 bytes saved thx @Dom Hastings

a=prompt`Enter String:`.replace(/[aeiou]/g,v=>(V[v]=-~V[v],''),V=[]);alert("Vowel Count: "+[...'aeiou'].map(v=>v+': '+(V[v]|0)).join`, `+`
Remaining characters: `+a)

\$\endgroup\$
  • \$\begingroup\$ Needs a space after the comma, but I'm surprised you didn't use a template string with a literal newline character. \$\endgroup\$ – Neil May 4 '16 at 0:06
  • 2
    \$\begingroup\$ @Neil I was careless, with so many unnecessary characters in the output, what's one more... \$\endgroup\$ – edc65 May 4 '16 at 6:25
  • \$\begingroup\$ A couple more bytes you can save, using a template string for the argument to prompt(...) and using |0 instead of ||0. \$\endgroup\$ – Dom Hastings May 5 '16 at 8:00
  • \$\begingroup\$ @DomHastings right, thanks \$\endgroup\$ – edc65 May 5 '16 at 8:25
  • \$\begingroup\$ You need a space after the colon in Enter String: \$\endgroup\$ – ericw31415 May 5 '16 at 19:32
3
\$\begingroup\$

Java, 272 bytes

class L{public static void main(String[]v){String p=v[0],o="Enter String: "+p+"\nVowel Count:",d=" ",e="aeiou";for(char c:e.toCharArray()){o+=d+c+": "+p.replaceAll("[^"+c+"]","").length();d=", ";}System.out.println("\nRemaining characters: "+p.replaceAll("["+e+"]",""));}}

Ungolfed

class L {
    public static void main(String[] v) {
        String input = v[0], result = "Enter String: " + input + "\nVowel Count:", delimiter = " ", vowels = "aeiou";
        for (char c : vowels.toCharArray()) {
            result += delimiter + c + ": " + input.replaceAll("[^" + c + "]", "").length();
            delimiter = ", ";
        }
        System.out.println("\nRemaining characters: " + input.replaceAll("[" + vowels + "]", ""));
    }
}

Notes

  • Input via program arguments: Save as L.java, compile with javac L.java, run with java L LoremIpsumDolorSitAmetConsecuteurAdipscingElit!

Output

Enter String: LoremIpsumDolorSitAmetConsecuteurAdipscingElit!
Vowel Count: a: 0, e: 4, i: 4, o: 4, u: 3
Remaining characters: LrmIpsmDlrStAmtCnsctrAdpscngElt!
\$\endgroup\$
  • \$\begingroup\$ I know it's a while, but you can golf it to 246 bytes like this: interface M{static void main(String[]v){String p=v[0],o="Enter String: "+p+"\nVowel Count:",d=" ",e="aeiou";for(String c:e.split("")){o+=d+c+": "+~-p.split(c,-1).length;d=", ";p=p.replace(c,"");}System.out.print(o+"\nRemaining characters: "+p);}} Try it here. \$\endgroup\$ – Kevin Cruijssen Nov 13 '17 at 8:31
2
\$\begingroup\$

Lua, 242 Bytes

For a moment, I thought that it will be longer than SQL... still a monster for such a simple task.

t={}x="aeiou"i=io.write 
x:gsub(".",function(c)t[c]=0 end)i("Enter string: ")z=io.read()s="Remaining characters: "..z:gsub(".",function(c)if x:find(c)then
t[c]=t[c]+1return""end end)i("Vowel Count: ")x:gsub(".",function(c)i(c..": "..t[c]..(c=="u"and"\n"or", "))end)i(s)

The job is done by the part

z:gsub(".",function(c)if x:find(c)then t[c]=t[c]+1return""end end)

Everything else is mainly for the purpose of outputing in the right format.

Ungolfed

t={}                             -- create an empty array
x="aeiou"                        -- list of vowels
i=io.write                       -- shorthand for outputing
x:gsub(".",function(c)t[c]=0 end)-- create a celle at 0 for each vowel
i("Enter string: ")              -- output some boilerplate
z=io.read()                      -- take the input
s="Remaining characters: "..     -- save in s the concatenation of this string
  z:gsub(".",function(c)         -- and the result of gsubing on each character of the input 
  if x:find(c)                   -- if the current char is a vowel
  then
    t[c]=t[c]+1                  -- increment its counter
    return""                     -- suppress it from the input
  end
end)

i("Vowel Count: ")               -- output some more boilerplate
x:gsub(".",function(c)           -- iterate over the vowels
  i(c..": "..t[c]..              -- output "l: #" appended with
    (c=="u"
        and"\n"                  -- "\n" if the current char is "u"
      or", "))                   -- ", " otherwise
end)
i(s)                             -- output the content of s
\$\endgroup\$
2
\$\begingroup\$

Perl 5, 127 bytes

$_=<>;print"Enter String: ${_}Vowel Count:";for$s(a,e,i,o,u){print" $s: ".~~s/$s//g,$s!~u&&","}print"
Remaining characters: $_"

Many thanks to Dom Hastings (in a comment hereon) for 14 bytes.

\$\endgroup\$
  • \$\begingroup\$ I had a slightly different approach using a loop, can you use the same mechanism to save bytes? sub p{print@_}p"Enter String: ";$_=<>;p"Vowel Count:";for$s(a,e,i,o,u){p" $s: ".~~s/$s//g,$s!~/u/&&","}p"\nRemaining characters: $_" (note: \n is a literal newline!) \$\endgroup\$ – Dom Hastings May 4 '16 at 9:23
  • \$\begingroup\$ @DomHastings, many thanks, that saves a bunch. \$\endgroup\$ – msh210 May 4 '16 at 15:20
2
\$\begingroup\$

PostgreSQL, 295, 263 bytes

SELECT v "Enter string:", string_agg(r,', 'ORDER BY n) "Vowel Count:",TRANSLATE(v,'aeyiou','') "Remaining characters:"
FROM (SELECT 'LoremIpsumDolorSitAmetConsecuteurAdipscingElit!'::text v)s
,LATERAL(SELECT LENGTH(v)l)a
,LATERAL(SELECT c||': '||l-LENGTH(REPLACE(v,c,''))r,n FROM (VALUES('a',1),('e',2),('i',3),('o',4),('u',5))t(c,n))b
GROUP BY v

SqlFiddleDemo

Output:

╔══════════════════════════════════════════════════╦═══════════════════════════════╦══════════════════════════════════╗
║                  Enter string:                   ║         Vowel Count:          ║      Remaining characters:       ║
╠══════════════════════════════════════════════════╬═══════════════════════════════╬══════════════════════════════════╣
║ LoremIpsumDolorSitAmetConsecuteurAdipscingElit!  ║ a: 0, e: 4, i: 4, o: 4, u: 3  ║ LrmIpsmDlrStAmtCnsctrAdpscngElt! ║
╚══════════════════════════════════════════════════╩═══════════════════════════════╩══════════════════════════════════╝    

Input: (SELECT '...'::text v)s

How it works:

  • (SELECT 'LoremIpsumDolorSitAmetConsecuteurAdipscingElit!'::text v)s input
  • ,LATERAL(SELECT LENGTH(v)l)a - length of input
  • (VALUES('a',1),('e',2),('i',3),('o',4),('u',5)) - vowels with oridinal
  • SELECT c||': '||l-LENGTH(REPLACE(v,c,''))r,n - calculate number of occurences
  • string_agg(r,', 'ORDER BY n) ... GROUP BY v combine result
  • TRANSLATE(v,'aeyiou','') string without vowels

EDIT 3:

Removed additional LATERAL, input moved to table.

SELECT v"Enter string:",string_agg(r,', 'ORDER BY n)"Vowel Count:",TRANSLATE(v,'aeyiou','')"Remaining characters:"
FROM s,LATERAL(SELECT c||': '||LENGTH(v)-LENGTH(REPLACE(v,c,''))r,n FROM(VALUES('a',1),('e',2),('i',3),('o',4),('u',5))t(c,n))b
GROUP BY v

SqlFiddleDemo

\$\endgroup\$
  • \$\begingroup\$ I'm still amazed how people use SQL for anything other than databases \$\endgroup\$ – Bálint May 6 '16 at 13:50
  • \$\begingroup\$ @Bálint In all my answers I use SQL 😎. In production I would not use this kind of code but as puzzle why not. \$\endgroup\$ – lad2025 May 6 '16 at 21:31
  • \$\begingroup\$ This would appear to hardcode the input and does not show the prompt required in the question. \$\endgroup\$ – cat May 8 '16 at 0:31
  • \$\begingroup\$ Output must look like this \$\endgroup\$ – cat May 8 '16 at 0:32
  • \$\begingroup\$ @cat Added version with table. Output must look like this with SQL (declarative statement) you cannot get interactive prompt. \$\endgroup\$ – lad2025 May 8 '16 at 5:31
2
\$\begingroup\$

Python, 220 201 bytes

This is my first submission on codegolf so go easy. :P

def f(y):
  global x;c=x.count(y);x=x.replace(y,'');return c;
x=input('Enter string: ');print('Vowel Count: a: {}, e: {}, i: {}, o: {}, u: {}'.format(*map(f,"aeiou")));print('Remaining characters: '+x)

Ungolfed:

def f(y):
  global x
  c=x.count(y)
  x=x.replace(y,'')
  return c;
x=input('Enter string: ')
print('Vowel Count: a: {}, e: {}, i: {}, o: {}, u: {}'.format(*map(f,"aeiou")))
print('Remaining characters: '+x)

Output:

Enter string: LoremIpsumDolorSitAmetConsecuteurAdipscingElit!
Vowel Count: a: 0, e: 4, i: 4, o: 4, u: 3
Remaining characters: LrmIpsmDlrStAmtCnsctrAdpscngElt!
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Golf! You might be able to save some bytes by using Python's list comprehensions and unpacking them into the format function instead of calling each function individually: ...u: {}'.format(*[f(c)for c in 'aeiou']). Also, I think you can put the commands from a function on the same line right after the colon and it should work, saving some bytes. \$\endgroup\$ – Value Ink May 7 '16 at 6:51
  • 2
    \$\begingroup\$ @KevinLau-notKenny It might be shorter to do .format(*map(f,"aeiou")) \$\endgroup\$ – cat May 8 '16 at 0:35
2
\$\begingroup\$

JavaScript, 190 186 bytes

(s,a=[..."aeiou"].map(v=>(s.match(v,"g")||[]).length))=>`Enter String: ${s}
Vowel Count: ${[..."aeiou"].map((v,i)=>v+": "+a[i]).join`, `}
Remaining characters: `+s.replace(/[aeiou]/g,"")

I just remembered that literal newlines are only 1 byte! Also remembered to golf something I missed before.

\$\endgroup\$
  • \$\begingroup\$ Why do you use RegExp(expression, modifier) instead of /expression/modifier? \$\endgroup\$ – Bálint May 6 '16 at 10:36
  • \$\begingroup\$ I need to plug in the v into the RegExp. If I used /v/g, the v would be a literal string instead of the value of the variable. \$\endgroup\$ – ericw31415 May 6 '16 at 11:55
1
\$\begingroup\$

PowerShell v2+, 196 149 bytes

$z=read-host "Enter string"
"Vowel Count: "+(([char[]]"aeiou"|%{$_+": "+($z.length-($z=$z-creplace$_).length)})-join', ')
"Remaining characters: "+$z

Takes input via read-host with a prompt, save into $z.

We then output a string literal "Vowel Count: " concatenated with the result of a loop/join through the vowels to construct the a: #, e: #,... string. This is done by taking the current string's .length minus the new string's .length after we perform a case-sensitive replacement with -creplace (i.e., how many did we remove). In addition, we re-save the result of the replace back into $z for the next loop.

We then also output the second string literal concatenated with whatever is still left in $z.

This keeps the current specification intact, in that capital vowels are distinguished from lowercase vowels, since -creplace is explicitly case sensitive.

Examples

PS C:\Tools\Scripts\golfing> .\briefest-code-find-vowels.ps1
Enter string: LoremIpsumDolorSitAmetConsecuteurAdipscingElit!
Vowel Count: a: 0, e: 4, i: 4, o: 4, u: 3
Remaining characters: LrmIpsmDlrStAmtCnsctrAdpscngElt!

PS C:\Tools\Scripts\golfing> .\briefest-code-find-vowels.ps1
Enter string: ProgrammingPuzzlesAndCodeGolf
Vowel Count: a: 1, e: 2, i: 1, o: 3, u: 1
Remaining characters: PrgrmmngPzzlsAndCdGlf
\$\endgroup\$
1
\$\begingroup\$

Retina, 133 bytes

(a()|e()|i()|o()|u()|.)+
Enter string: $&¶Vowel count: a: $#2, e: $#3, i:$#4, o:$#5, u:$#6¶Remaining characters: $&
(?<=s:.+)[aeiou]
<empty-line>

Try it online!

Who is more fit for this challenge than Retina?

\$\endgroup\$
1
\$\begingroup\$

Groovy, 181 177 bytes

online

​{i->r=[:].withDefault{0};o=i.toLowerCase().replaceAll(/[aeiou]/,{r[it]++;''})
"Enter String: $i\nVowel Count: ${r.collect{k,v->"$k: $v"}.join(' ')}\nRemaining characters: $o"}
\$\endgroup\$
1
\$\begingroup\$

Tcl, 184 (counting majuscule vowels) / 180 (ignoring them)

puts -nonewline {Enter String: }
flush stdout
gets stdin s
set c {Vowel Count:}
foreach v {a e i o u} {append c " $v: [regsub -all (?i)$v $s {} s]"}
puts "$c\nRemaining Characters: $s"

Explanation

Most of the code is actually dealing with the strict I/O requirements. The remainder works by doing a regular expression substitution (deletion) on the input string $s for every vowel and appending the vowel $v and associated number of substitutions to the output string $c.

The above code works for mixed-case vowels. Remove the (?i) option from the regular expression on the penultimate line for strictly minuscule vowels.

\$\endgroup\$
1
\$\begingroup\$

Python 162 Bytes

v,s="aeiou",input()
print "String:"+s+"\n"+"Vowel Count"+" ".join([c+":"+str(s.count(c)) for c in v])+"\n"+"Remaining Characters:"+filter(lambda x:v.count(x)<1,s)
\$\endgroup\$
1
\$\begingroup\$

C# 317 bytes

string t=Console.ReadLine();Dictionary v = new Dictionary<char,int>{{'a',0},{'e',0},{'i',0},{'o',0},{'u',0}};string o="";foreach(char c in t){if(v.Keys.Contains(c)){v[c]++;}else{o+=c;}}Console.WriteLine("a: "+v['a']+", e: "+v['e']+", i: "+v['i']+", o: "+v['o']+", u: "+v['u']);Console.WriteLine("Remaining Characters: "+o);

Any suggestions, trying to learn to golf with C#. 1KB is obviously way bigger than the other answers though.

Ungolfed:

string t=Console.ReadLine();
Dictionary v = new Dictionary<char,int>
{
    {'a',0},
    {'e',0},
    {'i',0},
    {'o',0},
    {'u',0}
};
string o="";
foreach(char c in t)
{
    if(v.Keys.Contains(c))
    {
        v[c]++;
    }
    else
    {
        o+=c;
    }
}
Console.WriteLine("a: "+v['a']+", e: "+v['e']+", i: "+v['i']+", o: "+v['o']+", u: "+v['u']);
Console.WriteLine("Remaining Characters: "+o);
\$\endgroup\$
  • \$\begingroup\$ Please specifiy an exact byte count in the future, 1 KB is way off from 317 bytes, wich isn't that bad. \$\endgroup\$ – Bálint May 6 '16 at 13:54
  • \$\begingroup\$ @Bálint Using school computer, it's cryptic getting to the properties of a file for some reason. \$\endgroup\$ – Robbie Coyne May 6 '16 at 13:59
  • \$\begingroup\$ I'd suggest using a regex ( /[aeiou]/ instead of a dictionary, an use that to match strings \$\endgroup\$ – Bálint May 6 '16 at 14:00
  • \$\begingroup\$ Just use an online byte counter. \$\endgroup\$ – Bálint May 6 '16 at 14:01
  • \$\begingroup\$ like mothereff.in/byte-counter \$\endgroup\$ – cat May 8 '16 at 0:37
0
\$\begingroup\$

Ruby, 202 bytes

print"Enter String: ";t=gets;c=->(x){t.chars.count(x)};a="aeiou".chars
s=a.zip(a.map(&c)).map{|e|"#{e[0]}: #{e[1]}"}.join(", ");r=t.gsub(/[aeiou]/,"")
puts"Vowel Count: #{s}\nRemaining characters: #{r}"
\$\endgroup\$
  • \$\begingroup\$ Use $><< instead of print. Do s="aeiou".chars.map{|e|"#{e}: #{t.count e}"}*", " to save a ton of bytes over setting up lambdas and zipping things together. Use t.tr('aeiou','') instead of gsub to replace, and maybe put it directly into where you have #{r} instead of assigning it. \$\endgroup\$ – Value Ink May 4 '16 at 0:01
0
\$\begingroup\$

C++, 449 bytes

Golfed:

#include <iostream>
#include <string>
int main(){std::string x;std::cout<<"Enter String: ",std::cin>>x;int a=0,e=0,i=0,o=0,u=0;for(int c=0;c<x.length();c++){switch(x[c]){case 'a':a++;x.erase(c,1);break;case 'e':e++;x.erase(c,1);break;case 'i':i++;x.erase(c,1);break;case 'o':o++;x.erase(c,1);break;case 'u':u++;x.erase(c,1);break;}}std::cout<<"\n Vowel Count: a: "<<a<<", e: "<<e<<", i: "<<i<<", o: "<<o<<", u: "<<u<<"\n Remaining characters: "<<x;}

Ungolfed:

#include <iostream>
#include <string>

int main(){
    std::string x;
    std::cout<<"Enter String: ", std::cin>>x;
    int a=0,e=0,i=0,o=0,u=0;
    for(int c = 0; c < x.length(); c++){
        switch(x[c]){
            case 'a':
                a++;
                x.erase(c,1);
                break;
            case 'e':
                e++;
                x.erase(c,1);
                break;
            case 'i':
                i++;
                x.erase(c,1);
                break;
            case 'o':
                o++;
                x.erase(c,1);
                break;
            case 'u':
                u++;
                x.erase(c,1);
                break;
        }
    }
    std::cout<<"\n Vowel Count: a: "<<a<<", e: "<<e<<", i: "<<i<<", o: "<<o<<", u: "<<u<<"\n Remaining characters: "<<x;
}
\$\endgroup\$
  • \$\begingroup\$ does case 'u', et al really need a space? I don't have a C compiler in front of me right now but I don't think it does. \$\endgroup\$ – cat May 8 '16 at 0:33
0
\$\begingroup\$

C++17, 313 bytes

#include <cctype>
#include <iostream>
#include <string>
using namespace std;int main(){int n,h[6]={0};string s,v="aeiou",r;cout<<"Enter String: ";getline(cin,s);for(c:s){h[n=v.find(tolower(c))+1]++;if(!n)r+=c;}cout<<"Vowel Count:";n=1;for(c:v)cout<<" "<<c<<": "<<h[n++];cout<<"\nRemaining Characters: "<<r<<"\n";}

I suppose I could make this just a function to eliminate standard boilerplate from the byte count.

Ungolfed

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int n;
  int histogram[6] = {0};   // { (consonants), A, E, I, O, U }
  string source;
  string vowels = "aeiou";  // vowel order matches the histogram
  string consonants;

  cout << "Enter String: ";
  getline( cin, source );

  for (c : source)
  {
    // n ← consonants=0, A=1, E=2, ...
    n = vowels.find( tolower(c) ) + 1;
    histogram[n]++;
    if (!n)
      consonants += c;
  }

  cout<<"Vowel Count:";
  n = 1;
  for (vowel : vowels)
    cout << " " << vowel << ": " << histogram[n++];

  cout << "\nRemaining Characters: " << consonants << "\n";
}

Explanation

Computes a histogram using the std::string lookup functions.

Again, you can eliminate 27 bytes from the count if only minuscule vowels matter. (Remove the first #include and the call to tolower().)

\$\endgroup\$
0
\$\begingroup\$

Ruby, 134 133 131 bytes

Because the other Ruby answer didn't respond to my suggestions, I golfed it down further myself.

-2 bytes from @manatwork

$><<"Enter String: "
r=gets.tr v='aeiou',''
puts"Vowel Count: #{v.chars.map{|e|e+": #{$_.count e}"}*', '}
Remaining characters: "+r
\$\endgroup\$
  • 1
    \$\begingroup\$ r=gets.tr v='aeiou','' \$\endgroup\$ – manatwork May 9 '16 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.