Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
class StringReverseProgram { static void Main(string[] args) { string normalStr = "This is DotNetMirror"; string reverseStr = string.Empty; foreach (char chr in normalStr) { reverseStr = chr + reverseStr; } Console.WriteLine(reverseStr); //Output : rorriMteNtoD si sihT Console.ReadKey(); } }
if (sum == numberToCheck) { Console.WriteLine("The given Number {0} is a Palindrome Number", numberToCheck); } else { Console.WriteLine("The given Number {0} is NOT a Palindrome Number", numberToCheck); } } }
Mark the constructor as private so that outside the class its not possible to create instance of the class. Create static/Shared method can be added to the class to create instance of the class and return the reference of the object. class SingletonClass { private static SingletonClass obj; public int tempVal { get; set; } //private constuctor private SingletonClass() { } public static SingletonClass GetInstance() { if (obj == null) { obj = new SingletonClass(); } return obj; } }
class SingletonClassProgram { public static void Main(string[] args) { SingletonClass obj1,obj2; //obj1=new SingletonClass(); //invlid due to constructore is private. Error as inaccissible due to protection level obj1=SingletonClass.GetInstance(); obj1.tempVal =20; obj2=SingletonClass.GetInstance(); Console.WriteLine(" /********* singleton class Example **********/"); Console.WriteLine("variable value is {0}",obj2.tempVal); //value is 20 because both obj1,obj2 are referring to same object Console.Read(); } }
Reverse only the word in the string not the whole string without using inbuilt functions
class Palindrome { public static void Main() { string text = "C Sharp Corner "; Console.WriteLine(PrintWordInReverse(text)); } //Method to reverse the order static string PrintWordInReverse(string str) { string revWord = string.Empty; string finalsentence = string.Empty; for (int i = 0; i < str.Length; i++) { if(str[i]!=null) { revWord=str[i]+revWord; } else { revWord=revWord+""; finalsentence=finalsentence+revWord; revWord=string.Empty;