C#
C#

help

Root Question Message

Kryca
Kryca12/6/2022
❔ 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;
        }

char[] balse = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' };
Any help is appreciated
AntonC
AntonC12/6/2022
yeah seems good
AntonC
AntonC12/6/2022
the best way would be to use Array.IndexOf or a binary search maybe
Kryca
Kryca12/6/2022
Wouldn't my code count duplicates too? i only need to know the amount of unique vowels
AntonC
AntonC12/6/2022
oh you don't, alright
AntonC
AntonC12/6/2022
well probably just mark a vowel whener you find one
AntonC
AntonC12/6/2022
with another bool[] or BitArray or just by marking bits on an int
Aimbot
Aimbot12/6/2022
Well you can use linq but I don't know if it's worth for you to learn it rn
AntonC
AntonC12/6/2022
or a HashSet<char>, or a List<char>
AntonC
AntonC12/6/2022
there are a bunch of ways, the int with marking bits is probably the fastest
Kryca
Kryca12/6/2022
Could you type an example? haven't really dealt with text editing that much
AntonC
AntonC12/6/2022
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
AntonC
AntonC12/6/2022
with an int
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;
}

same with bool[]
AntonC
AntonC12/6/2022
it's a general thing, it has nothing to do with text editing in particular
Kryca
Kryca12/6/2022
Just tried it out and it works, thanks a lot ;D
AntonC
AntonC12/6/2022
np
AntonC
AntonC12/6/2022
note that the first one is undefined behavior if the length of balse is more than 32
AntonC
AntonC12/6/2022
be sure to use bit operations carefully
Kryca
Kryca12/6/2022
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 ?
AntonC
AntonC12/6/2022
split by new line characters, swap the needed lines, join the text back
AntonC
AntonC12/6/2022
but if you're doing text editing software, you probably should store it split by linea internally
AntonC
AntonC12/6/2022
that would make sense either way
Kryca
Kryca12/6/2022
i know how to split text into lines words etc, how do i move it to the top though
AntonC
AntonC12/6/2022
I think
Kryca
Kryca12/6/2022
all the lines asides from the one i want to move up should stay in the same position
AntonC
AntonC12/6/2022
the same way you swap values of two variables, swap the values of two positions in the array
AntonC
AntonC12/6/2022
oh hold on
AntonC
AntonC12/6/2022
you mean you want to move the rest below them
Kryca
Kryca12/6/2022
say these are the lines
1 yes
2yup
3 maybe
4 no

this should be the output:
3 maybe
1 yes
2yup
4 no
AntonC
AntonC12/6/2022
1
2
3
4

3
4
1
2

let's say these are the lines
AntonC
AntonC12/6/2022
right?
Kryca
Kryca12/6/2022
pretty much instead of moving 2 i need to move 1 only
AntonC
AntonC12/6/2022
yeah with a single line moving it's a bit simpler than moving a chunk
Kryca
Kryca12/6/2022
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
AntonC
AntonC12/6/2022
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
Kryca12/6/2022
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 ?
Kryca
Kryca12/6/2022
or is it easier in c# ?
AntonC
AntonC12/6/2022
double for?
Kryca
Kryca12/6/2022
two 'for' cycles
AntonC
AntonC12/6/2022
you only need a single for
AntonC
AntonC12/6/2022
to move the lines one up
AntonC
AntonC12/6/2022
you could also just use a list
AntonC
AntonC12/6/2022
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
AntonC
AntonC12/6/2022
this moves twice as many items on average
Kryca
Kryca12/6/2022
would lines.ReadAllLines(filename); work to fill up the list?
AntonC
AntonC12/6/2022
ah no, three times as much
Kryca
Kryca12/6/2022
oh wait no
AntonC
AntonC12/6/2022
 lines.AddRange(File.ReadAllLines(filename));
AntonC
AntonC12/6/2022
but just move manually, it's like 4 lines of code
arttt
arttt12/6/2022
LINQ:

 
string uniqueVowels = new string(input.Where(c => vowels.Contains(c)).Distinct().ToArray());
Kryca
Kryca12/6/2022
Aight, thanks a lot for the help :D
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy