I'm stuck at this problem and I don't know how to solve it.
A Romanian text is written in gibberish by taking the original text and inserting after each vowel the letter p and the corresponding vowel.
Data input:
Apanapa aparepe meperepe.
Must return:
Ana are mere.
Here is my code
string s = Console.ReadLine();
int i = 0; int k = 0; bool b = true;
string s2 = s;
while (i < s.Length)
{
b = false;
if (s[i] == 97 || s[i] == 65 || s[i] == 101 || s[i] == 69 || s[i] == 105 || s[i] == 73 || s[i] == 111 || s[i] == 79 || s[i] == 117 || s[i] == 85 || s[i] == 99)
{
b = true;
}
if (!b)
{
s2 = s2.Substring(0, k) s.Substring(k);
i ;
k ;
}
else
{
s2 = s2.Substring(0, i) s2[i] s.Substring(i 3);
i = i 3;
k ;
}
}
Console.WriteLine(s2);
Console.Read();
CodePudding user response:
A global piece of advice: when you need to remove entries from a collection, you need to go from end to beginning, not from beginning to end, in pseudocode:
string input = "Apanapa aparepe meperepe";
string temp = input;
for (int i = input.Length();i=0;i--)
if (temp.get(i) == 'p') and (i != 0) and (isVowel(temp.get(i-1)))
temp.remove(i);
If you go from beginning to end, your i
(index) might get lost :-)
CodePudding user response:
string s = Console.ReadLine();
string s2 = string.Empty;
for(int i = 0; i < s.Length; i )
{
char currentChar = s[i];
if(currentChar == 'p' || currentChar == 'P' )
{
char prevChar = '\0';
char nextChar = '\0';
if(i - 1 >= 0)
prevChar = s[i - 1];
if(i 1 < s.Length)
nextChar = s[i 1];
if(Char.ToLower(nextChar) == Char.ToLower(prevChar))
{
i = 1;
continue;
}
}
else
{
s2 = currentChar;
}
}
Console.WriteLine(s2);
Many ways to rome. There is another way to see the problem ¯\(ツ)/¯
CodePudding user response:
I am not sure if you need help with the code or with the Logic. One thing you can do is simply iterate through the string and build a new string while jumping two steps everytime you encounter a vowel.
So something like this should work ( I might have missed some edge cases here and there)
string s = Console.ReadLine();
string sol = "";
for (int i=0;i<s.Length;i )
{
if (s[i] == 97 || s[i] == 65 || s[i] == 101 || s[i] == 69 || s[i] == 105 || s[i] == 73 || s[i] == 111 || s[i] == 79 || s[i] == 117 || s[i] == 85 || s[i] == 99)
{
sol = s[i];
i = 2;
}
else
sol = s[i];
}
Console.WriteLine(sol);
Console.ReadLine();