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)'
}
}
}
Hemant SrivastavaPosted Sep 27, 2013, 2:18 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solution
{
class Program
{
public static bool solution(String S, ref int middleCharPostion)
{
bool IsValidString = false;
int mid = -1;
if (S.Length % 2 == 0)
{
IsValidString = false;
}
else
{
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)
{
IsValidString = true;
middleCharPostion = mid + 1;
}
else
{
IsValidString = false;
middleCharPostion = -1;
}
}
return IsValidString;
}
static void Main(string[] args)
{
int midCharacterPosition = -1;
Console.WriteLine("Enter a string :");
string S = Console.ReadLine();
if (solution(S, ref midCharacterPosition) == true)
{
if (midCharacterPosition > 0)
{
Console.WriteLine("Entered string is valid. and Index Position of the middle character ["+ S[midCharacterPosition-1]+"] is: " + midCharacterPosition);
}
else
{
Console.WriteLine("Entered string is not a valid string.");
}
}
else
{
Console.WriteLine("Entered string is not a valid string.");
}
Console.Read();
}
}
}
Hemant SrivastavaPosted Sep 27, 2013, 1:44 PM
VulpesPosted Sep 27, 2013, 1:41 PM
Also, since the solution method isn't static, you'll need to create an instance of the Program class to access it.
So try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solution
{
class Program
{
public void 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 ;
}
static void Main(string[] args)
{
Console.WriteLine("enter a string :");
string S = Console.ReadLine();
Program p = new Program();
p.solution(S);
}
}
}