Home > Back-end >  How do I make my own String.Split() and Array.Reverse() built-in functions in one user-defined funct
How do I make my own String.Split() and Array.Reverse() built-in functions in one user-defined funct

Time:06-12

I have tried this:

using System;
using System.Collections;
using System.Collections.Generic;

public class HelloWorld
{
    public static string reverseWords(string str){
        ArrayList strArr = new ArrayList();
        int start = 0;
        string revStr = "";
        for(int i = 0; i < str.Length; i  ){ 
            if(str[i] == ' '){               // if there's a space,
                while(start <= str[i - 1]){  // loop thru the iterated values before space
                    strArr.Add(str[start]);  // add them to the ArrayList
                    start  ;                 // increment `start` until all iterated values are-
                }                            // stored and also for the next word to loop thru
            }
        }
        for(int j = strArr.Count - 1; j >= 0;  j--){
            revStr  = strArr[j]   " ";             // keep appending ArrayList values to the-
        }                                          // string from the last to the first value
        return revStr;
    }
    
    public static void Main(string[] args)
    {
       Console.WriteLine(reverseWords("Our favorite color is Pink"));
       //Expected output : Pink is color favorite Our 
    }
}

And it's giving this error:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

Please help me understand why this is not working. And also, if there's better way to do this ReverseWord function manually(not using any built-in functions at all).

I'm sorry if this is such a noob question. Any constructive criticism is appreciated. Thanks!

CodePudding user response:

Here is a little improved version of your code that actually works for what you are willing to do.

using System;
using System.Collections;

public class HelloWorld
{
    public static string reverseWords(string str){
        ArrayList strArr = new ArrayList();
        string currentWordString = string.Empty; 
        string revStr = string.Empty;
        for(int i = 0; i < str.Length; i  ){ 
            if(str[i] == ' '){               // if there's a space,
                strArr.Add(currentWordString); // add the accumulated word to the array
                currentWordString = string.Empty; // reset accumulator to be used in next iteration
            }else {
                currentWordString  = str[i]; // accumulate the word
            }
        }
        
        strArr.Add(currentWordString); // add last word to the array
        
        
        for(int j = strArr.Count - 1; j >= 0;  j--){
            revStr  = strArr[j]   " ";             // keep appending ArrayList values to the-
        }                                          // string from the last to the first value
        return revStr;
    }
    
    public static void Main(string[] args)
    {
       Console.WriteLine(reverseWords("Our favorite color is Pink"));
       //Expected output : Pink is color favorite Our 
    }
}

I'll let you do the remaining. Like removing the trainling space at the end of the sentence. add seperators other than space (e.g comma, semicolons...)

CodePudding user response:

Try this

 "Our favorite color is Pink".Split('\u0020').Reverse().ToList().ForEach(x =>
  {
      Console.WriteLine(x);
  });
  • Related