Saransh Lakra
Saransh Lakra

Reputation: 52

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.

Upvotes: 0

Views: 78

Answers (2)

Hitesh
Hitesh

Reputation: 36

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;

           
        }

Upvotes: 1

Idle_Mind
Idle_Mind

Reputation: 39132

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...

Upvotes: 3

Related Questions