C
C#2y ago
kryca

❔ How do i count how many different vowels are in a string?

The only method i've come up with is to count how many vowels are in a string not accounting for duplicates. This is the code so far:

static int DiffVowel(string e, char[] balse)
{
int z = 0;
foreach (char c in e)
{
for (int i = 0; i < balse.Length; i++)
{
if (balse[i] == c)
{
z++;
}
}
}
return z;
}

static int DiffVowel(string e, char[] balse)
{
int z = 0;
foreach (char c in e)
{
for (int i = 0; i < balse.Length; i++)
{
if (balse[i] == c)
{
z++;
}
}
}
return z;
}
char[] balse = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' }; Any help is appreciated
43 Replies
Anton
Anton2y ago
yeah seems good the best way would be to use Array.IndexOf or a binary search maybe
kryca
krycaOP2y ago
Wouldn't my code count duplicates too? i only need to know the amount of unique vowels
Anton
Anton2y ago
oh you don't, alright well probably just mark a vowel whener you find one with another bool[] or BitArray or just by marking bits on an int
Aimbot
Aimbot2y ago
Well you can use linq but I don't know if it's worth for you to learn it rn
Anton
Anton2y ago
or a HashSet<char>, or a List<char> there are a bunch of ways, the int with marking bits is probably the fastest
kryca
krycaOP2y ago
Could you type an example? haven't really dealt with text editing that much
Anton
Anton2y ago
so like when you find a vowel at position N, set the bit at position N in that int. If it's already set, don't increment the count with an int
int seen = 0;
// ...
if (balse[i] == c
&& ((seen & (1 << i)) == 0))
{
z++;
seen |= 1 << i;
}
int seen = 0;
// ...
if (balse[i] == c
&& ((seen & (1 << i)) == 0))
{
z++;
seen |= 1 << i;
}
with a bit array
var seen = new BitArray(balse.Length);
// ..
if (balse[i] == c && !seen[i])
{
z++;
seen[i] = true;
}
var seen = new BitArray(balse.Length);
// ..
if (balse[i] == c && !seen[i])
{
z++;
seen[i] = true;
}
same with bool[] it's a general thing, it has nothing to do with text editing in particular
kryca
krycaOP2y ago
Just tried it out and it works, thanks a lot ;D
Anton
Anton2y ago
np note that the first one is undefined behavior if the length of balse is more than 32 be sure to use bit operations carefully
kryca
krycaOP2y ago
Another question then, say i have 3 lines of text, and i want to move the 2nd line all the way to the top, how should i approach this ?
Anton
Anton2y ago
split by new line characters, swap the needed lines, join the text back but if you're doing text editing software, you probably should store it split by linea internally that would make sense either way
kryca
krycaOP2y ago
i know how to split text into lines words etc, how do i move it to the top though
Anton
Anton2y ago
I think
kryca
krycaOP2y ago
all the lines asides from the one i want to move up should stay in the same position
Anton
Anton2y ago
the same way you swap values of two variables, swap the values of two positions in the array oh hold on you mean you want to move the rest below them
kryca
krycaOP2y ago
say these are the lines 1 yes 2yup 3 maybe 4 no this should be the output: 3 maybe 1 yes 2yup 4 no
Anton
Anton2y ago
1 2 3 4 3 4 1 2 let's say these are the lines right?
kryca
krycaOP2y ago
pretty much instead of moving 2 i need to move 1 only
Anton
Anton2y ago
yeah with a single line moving it's a bit simpler than moving a chunk
kryca
krycaOP2y ago
its a weird task with a bit of limitations, but to put it simply, find the longest word in a text file, move that line that has the longest word to the top
Anton
Anton2y ago
the idea is to remember the line you want to move, then move all the lines before it one up, then write it into the first postion
kryca
krycaOP2y ago
okay so treat the lines of the text as an array and then simply make space for a line at the start with double for ? or is it easier in c# ?
Anton
Anton2y ago
double for?
kryca
krycaOP2y ago
two 'for' cycles
Anton
Anton2y ago
you only need a single for to move the lines one up you could also just use a list
var lines = List<string>();
// fill up the lines list
var longestIndex = FindIndexOfLongest(lines);
var longest = lines[longestIndex];
lines.RemoveAt(longestIndex);
lines.Insert(0, longest);
var lines = List<string>();
// fill up the lines list
var longestIndex = FindIndexOfLongest(lines);
var longest = lines[longestIndex];
lines.RemoveAt(longestIndex);
lines.Insert(0, longest);
it does a bit more work than moving stuff manually tho this moves twice as many items on average
kryca
krycaOP2y ago
would lines.ReadAllLines(filename); work to fill up the list?
Anton
Anton2y ago
ah no, three times as much
kryca
krycaOP2y ago
oh wait no
Anton
Anton2y ago
lines.AddRange(File.ReadAllLines(filename));
lines.AddRange(File.ReadAllLines(filename));
but just move manually, it's like 4 lines of code
artya
artya2y ago
LINQ:

string uniqueVowels = new string(input.Where(c => vowels.Contains(c)).Distinct().ToArray());

string uniqueVowels = new string(input.Where(c => vowels.Contains(c)).Distinct().ToArray());
kryca
krycaOP2y ago
Aight, thanks a lot for the help :D
Anton
Anton2y ago
just do a ToHashSet() and keep it a set whats the point of converting it to a string if you've already got a set
artya
artya2y ago
hmm true
Anton
Anton2y ago
(distinct makes a set internally)
ero
ero2y ago
input.ToLower()
.ToHashSet()
.Intersect(vowels) // only need the lowercase ones
.Count()
input.ToLower()
.ToHashSet()
.Intersect(vowels) // only need the lowercase ones
.Count()
kryca
krycaOP2y ago
kryca
krycaOP2y ago
kryca
krycaOP2y ago
n is 5, my text file has 7 lines
ero
ero2y ago
error is pretty clear, no? i mean evidently it does not
kryca
krycaOP2y ago
ye nvm mixed up the names of the text files...
softmek
softmek2y ago
string inputString; inputString = Console.ReadLine(); int vowelCount = 0; foreach (char c in inputString) { if (c == 'a' c == 'e' c == 'i' c == 'o' c == 'u') { vowelCount++; } } Console.WriteLine(vowelCount);
Aimbot
Aimbot2y ago
@softmek It's neither concise nor fast solution and it counts duplicates too You can immediately get the count without storing the score in the string
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server