Home > Blockchain >  How to delete specified symbol in every occurence without losing index
How to delete specified symbol in every occurence without losing index

Time:03-29

Ok so I am trying to make "Mexican wave" and code works but I have a problem when words have space between them because indexes are no longer the same after changing it from foreach to trim i realised that it doesn't work inside sentence for example "Two words" this space won't be removed. This is importat that the spaces have to be added to the words at exact index where they were because that is one of tests. So that is it i am out of ideas...

public static List<string> wave(string str)
{
    List<string> wave = new List<string>();

    //finding every occurence of space in str
    var foundIndexes = new List<int>();
    for (int i = 0; i < str.Length; i  )
    {
        if (str[i] == ' ')
            foundIndexes.Add(i);
    }

    //deleting spaces 
    str = str.Trim(' '); 

    // OR this one //
    //foreach(int a in foundIndexes)
    //{
    //    str = str.Remove(a, 1);
    //}
    

    //changing str to charArray
    char[] letters = str.ToCharArray();

    //here magic is being made
    for (int i = 0; i < letters.Length; i  )
    {
        if (i == 0)
        {
            string fn = str.Substring(i   1);
            string addStr = letters[i].ToString().ToUpper()   fn;
            foreach (int a in foundIndexes)
            {
                addStr = addStr.Insert(a, " ");
            }
            wave.Add(addStr);
        }
        else
        {
            if (char.IsUpper(wave[i - 1][i - 1]) || char.IsUpper(wave[i - 1][i]))
            {
                string CopyOfStr = str.Remove(i, 1);
                string UpperLetter = letters[i].ToString().ToUpper();
                string AddStrE = CopyOfStr.Insert(i, UpperLetter);

                foreach (int a in foundIndexes)
                {
                    AddStrE = AddStrE.Insert(a, " ");
                }
                wave.Add(AddStrE);
            }
        }
    }

    return wave;
}

If you have better idea how to make this I would love to hear :3

Example output :

 wave("hello") => []string{"Hello", "hEllo", "heLlo", "helLo", "hellO"}

CodePudding user response:

Are there other requirements you aren't listing?

Why not simply?

public static List<string> wave(string str)
{
    // Assumes input is lowercase and contains only letters and spaces.
    List<string> waves = new List<string>();
    StringBuilder sb = new StringBuilder(str);
    for (int i = 0; i < sb.Length; i  )
    {
        if (char.IsLetter(sb[i]))
        {
            sb[i] = char.ToUpper(sb[i]);
            waves.Add(sb.ToString());
            sb[i] = char.ToLower(sb[i]);
        }
    }
    return waves;
}

Example usage:

string[] inputs = { "hello", " gap", "two words" };
foreach(string input in inputs)
{
    Console.WriteLine(input   " --> "   String.Join(", ", wave(input)));
}    

Producing:

hello --> Hello, hEllo, heLlo, helLo, hellO
 gap -->  Gap,  gAp,  gaP
two words --> Two words, tWo words, twO words, two Words, two wOrds, two woRds, two worDs, two wordS

CodePudding user response:

This might be messy, but it works:

public static List<string> Wave(string str)
        {
            List<string> result = new List<string>();
            var words = str.ToLower().Split(" ", StringSplitOptions.RemoveEmptyEntries);
            string wordsAfterCurrent = string.Empty;
            string wordsBeforecurrent = string.Empty;
            for (int i = 0; i < words.Length; i  )
            {
                if (i == 0 && words.Length != 1)
                {
                    wordsAfterCurrent = words[(i 1)..(words.Length-i)].Aggregate((s1, s2) => s1   " "   s2);
                    result.AddRange(GetWordVariations(words[i]).Select(variation => variation   " "   wordsAfterCurrent));
                }
                else if (i == 0 && words.Length == 1)
                {
                    result.AddRange(GetWordVariations(words[i]).Select(variation => variation   " "   wordsAfterCurrent));
                }
                else if (i == words.Length - 1)
                {
                    wordsBeforecurrent = words[0..(i)].Aggregate((s1, s2) => s1   " "   s2);
                    result.AddRange(GetWordVariations(words[i]).Select(variation => wordsBeforecurrent   " "   variation));
                }
                else
                {
                    wordsAfterCurrent = words[(i)..(words.Length - i   1)].Aggregate((s1, s2) => s1   " "   s2);
                    wordsBeforecurrent = words[0..(i)].Aggregate((s1, s2) => s1   " "   s2);
                    result.AddRange(GetWordVariations(words[i]).Select(variation => wordsBeforecurrent   " "   variation   " "   wordsAfterCurrent));
                }

            }

            return result;
        }

        public static List<string> GetWordVariations(string word)
        {
            var letters = word.ToCharArray();
            List<string> result = new List<string>();
            for (int i = 0; i < letters.Length; i  )
            {
                if (i == 0)
                {
                    result.Add(letters[i].ToString().ToUpper()   word.Substring(i   1));
                }
                else
                {
                    result.Add(word.Substring(0, i)   letters[i].ToString().ToUpper()   word.Substring(i   1));
                }
            }
            return result;
        }
  •  Tags:  
  • c#
  • Related