A Place to Learn Ur-self…


  1. static void Main(string[] args)
  2.         {
  3.             string inp = Console.ReadLine();
  4.             StringBuilder strb = new StringBuilder();
  5.             List<char> word = new List<char>();
  6.             for (int c = 0; c < inp.Length; c++)
  7.             {
  8.  
  9.                 if (char.IsWhiteSpace(inp[c]) == true || c == inp.Length)
  10.                 {
  11.                     string tsb = string.Empty;
  12.                     foreach (var item in word)
  13.                     {
  14.                         tsb += item;
  15.                     }
  16.                     strb.Insert(0, tsb);
  17.                     strb.Insert(0, ' ');
  18.                     word = new List<char>();
  19.                 }
  20.                 else
  21.                     word.Add(inp[c]);
  22.             }
  23.             string tsb1 = string.Empty;
  24.             foreach (var item in word)
  25.             {
  26.                 tsb1 += item;
  27.             }
  28.             strb.Insert(0, tsb1);
  29.             string output = strb.ToString();
  30.             Console.WriteLine(output);
  31.             Console.Read();
  32.         }

OUT PUT:

input string: This is nice blog

output string: blog nice is This


Using string split function:

  1. static void Main(string[] args)
  2.         {
  3.             string str = Console.ReadLine();
  4.             string[] words = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  5.             Console.WriteLine("No of words in \"{0}\" are {1}", str, words.Length);
  6.             Console.Read();
  7.         }

Find number of words without using string.split function:

Using Regex:
  1. static void Main(string[] args)
  2.         {
  3.             string str = Console.ReadLine();
  4.             var words = Regex.Matches(str, @"[\S]+");
  5.             Console.WriteLine("No of words in \"{0}\" are {1}", str, words.Count);
  6.             Console.Read();
  7.         }

Using whitespace:
  1. string str = Console.ReadLine();
  2.             int words = 1;
  3.             for (int i = 0; i < str.Length – 1; i++)
  4.             {
  5.                 if (char.IsWhiteSpace(str[i]) == true)
  6.                 {
  7.                     words++;
  8.                 }
  9.             }
  10.             Console.WriteLine("No of words in \"{0}\" are {1}", str, words);
  11.             Console.Read();






Example1:

  1. String rawXml =
  2.           @"<root>
  3.                 <person firstname=""Riley"" lastname=""Scott"" />
  4.                 <person firstname=""Thomas"" lastname=""Scott"" />
  5.             </root>";
  6.             XmlDocument xmlDoc = new XmlDocument();
  7.             xmlDoc.LoadXml(rawXml);

Example2:

  1. String rawXml =
  2.           @"<root>
  3.                 <person firstname=""Riley"" lastname=""Scott"" />
  4.                 <person firstname=""Thomas"" lastname=""Scott"" />
  5.             </root>";
  6.             XElement xmlDoc = XElement.Parse(rawXml);

Example3:

  1. String rawXml =
  2.           @"<root>
  3.                 <person firstname=""Riley"" lastname=""Scott"" />
  4.                 <person firstname=""Thomas"" lastname=""Scott"" />
  5.             </root>";
  6.             XDocument xd = XDocument.Parse(rawXml);