Tech Point Fundamentals

Friday, November 26, 2021

C# Program to Find the No of Words in a String

C# Program to Find the No of Words in a String


Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor. 

This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.

Watch our videos here


How can you find the total number of words in a given sentence? Write a program to find the no of words in a given sentence.

C# Program To Find the No of Word in a String


// Author 	: Tech Point Fundamentals
// Website 	:
// Channel	:
// Demo		: Number of Words in String

using System;

public class Program
 public static void Main()
   string inputString = "Tech Point Fundamentals";		
   Console.WriteLine("Input String : " + inputString);
   Console.WriteLine("Word Counts With Split Function : " + WordCount(inputString));
   Console.WriteLine("Word Counts Without Split Function : " + WordCountWithoutSplitFunction(inputString));
 public static int WordCount(string inputString)    
  string[] words = inputString.Split(' ');   
  return words.Length;
 public static int WordCountWithoutSplitFunction(string inputString)    
   int wordCount = 1;
   char[] charArray = inputString.Trim().ToCharArray(); 
   foreach(char item in charArray)
   	if(item == ' ')
   		wordCount = wordCount + 1;
   return wordCount;


                Input String : Tech Point Fundamentals
Word Counts With Split Function : 3
Word Counts Without Split Function : 3

Live Demo

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.