Remove extra space from string without using inbuilt function.

Below program is for remove extra space from given string.it will remove extra space from given words and give you formatted output.
Hope you will enjoy it.

class Program
    {
        static void Main(string[] args)
        {
            string s = "My name  is    john";
            string final = "";
            int spacecount = 0;
            foreach (char c in s)
            {
                string isEmpty = c.ToString();
                if (isEmpty == " ")
                {
                    spacecount++;
                    continue;
                }
                if (spacecount >= 1)
                {
                    spacecount = 0;
                    final += " ";
                }
                final += c;
             

            }
            Console.Write(final);
            Console.ReadLine();
        }

    }

Output:My Name is john

No comments:

Post a Comment

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...