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

3
  • You've declared the method return type as string, but you're returning a char[]. Commented Feb 25, 2022 at 0:42
  • 1
    all this code does is copy the input to the output, there is no searching. I assume this doesnt compile, to make it compile do return new string(ch); Commented Feb 25, 2022 at 0:45
  • 1
    the question is not api specific, please post a simple console application instead code snippet of web api. Commented Feb 25, 2022 at 1:04

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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;

           
        }

1 Comment

This is wrong. A string[] is not the right type here, nor is this what the OP wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.