Pandu

Pandu

  • NA
  • 18
  • 19.6k

C# call by ref function

Sep 27 2013 1:10 PM
Following is my c# code. I have a solution function which has return type int and a string is passed through it. My aim is to get the middle index character in the string and left side of string should be reverse of right side of the string. eg 'racEcar'. In this E is middle character and rac and car are reverse substrings. I am getting errors at return statement and in Main when I call my solution fucntion passing the parameter as 'S' I am not able to get that basic mistake I am doing. 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solution
{
    class Program
    {
        public int solution(String S)
        {
            int mid = S.Length / 2;
            string leftS = S.Substring(0, mid);
            string revleftS = "";
            for (int index = leftS.Length - 1; index >= 0; index--)
            {
                revleftS += leftS[index];
            }

            string rightS = S.Substring(mid + 1);

            if (revleftS.CompareTo(rightS) == 0)
            {
                Console.WriteLine(mid);
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("left and right part of strings are not equal : -1");
                Console.ReadKey();
            }

            return ; //here I am getting error
        }

        static void Main(string[] args)
        {
            
            Console.WriteLine("enter a string :");
            string S = Console.ReadLine();
            Convert.ToInt32(solution(S)); //here I am getting error as object reference is required for the non-static field, method, or property //'Solution.Program.solution(string)'
        }
    }
}

Answers (3)