Write a program in C# Sharp to count the total number of words in a string

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to count the total number of words in a string.

Test Data :
Input the string : This is w3resource.com
Expected Output :

Total number of words in the string is : 3 

الأجوبة

using System;  
public class Exercise5
{  
    public static void Main() 
{
    string str;
    int i, wrd,l;	
      Console.Write("\n\nCount the total number of words in a string :\n");
      Console.Write("------------------------------------------------------\n"); 	
      Console.Write("Input the string : ");
      str = Console.ReadLine();	
    l = 0;
    wrd = 1;
    /* loop till end of string */
    while (l <= str.Length - 1)
    {
        /* check whether the current character is white space or new line or tab character*/
        if(str[l]==' ' || str[l]=='\n' || str[l]=='\t')
        {
            wrd++;
        }
        l++;
    }
   Console.Write("Total number of words in the string is : {0}\n", wrd);
	}
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...