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
{
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.