'How to store a string from user input in an array to perform search operation on it in C#?

 public class CellPhoneMessagingController : ApiController
    {
        [HttpGet]
        [Route("api/CellPhoneMessaging/{code}")]
        public string burgers(string code)
        {

          string userCode = code;
            char[] ch = new char[userCode.Length];
            for (int i = 0; i < ch.Length; i++) { 
                    ch[i] = userCode[i];
            }

            return ch;

           
        }
    }

I tried this but it is not returning the ch. Note: I am new in programming.



Solution 1:[1]

If you want it to return the characters of the string as an array, then use String.ToCharArray():

public char[] burgers(string code)
{
    return code.ToCharArray();
}

Seems kinda pointless now to put that into a function, though...

Solution 2:[2]

The return type has a problem. Your function should have a return type of string[].

 public class CellPhoneMessagingController : ApiController
    {
        [HttpGet]
        [Route("api/CellPhoneMessaging/{code}")]
        public string[] burgers(string code)
        {

          string userCode = code;
            char[] ch = new char[userCode.Length];
            for (int i = 0; i < ch.Length; i++) { 
                    ch[i] = userCode[i];
            }

            return ch;

           
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Idle_Mind
Solution 2 Hitesh