We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Sunday, May 29, 2016

Write a program give a string and if first char of every word is capital then it is to be small character and if first char of every word is small then it is to be capital character in c#

using System;
using static System.Console;

namespace CSarpExamples
{
    class UpperLowerchar
    {
        public static string ToUpper(string str)
        {
            string result = str;
            if (!string.IsNullOrEmpty(str))
            {
                var word = str.Split(' ');
                for (int i = 0; i < word.Length; i++)
                {
                    string s = word[i];
                    if (s.Length > 0)
                    {
                        word[i] = s[0].ToString().ToUpper() + s.Substring(1);
                    }
                }

                result = string.Join(" ", word);
            }
            return result;
        }
        public static string ToLower(string str)
        {
            string result = str;
            if (!string.IsNullOrEmpty(str))
            {
                var word = str.Split(' ');

                for (int i = 0; i < word.Length; i++)
                {
                    string s = word[i];

                    if (s.Length > 0)
                    {
                        word[i] = s[0].ToString().ToLower() + s.Substring(1);

                    }
                }
                result = string.Join(" ", word);

            }
            return result;
        }

        static void Main()
        {
            string FinalResult;
            NextLine:
            Console.WriteLine("Enter the string");
            string result = ReadLine();

            FinalResult = result;
            if (FinalResult == result.ToUpper())
            {
                var testResult = ToLower(FinalResult);
                WriteLine(testResult);
                WriteLine();
            }
            else if (FinalResult == result.ToLower())
            {
                var testResult = ToUpper(FinalResult);
                WriteLine(testResult);
                goto NextLine;
            }
            ReadLine();
        }

    }


}

Output:

No comments: