প্রথমে স্ট্রিং ডিক্লেয়ার করুন:
string str = "Hello World!";
পূর্ণ স্ট্রিংয়ের মাধ্যমে লুপ চালিয়ে হোয়াইটস্পেস, ট্যাব বা নিউলাইন ক্যারেক্টার খুঁজুন:
while (a <= str.Length - 1) {
if (str[a] == ' ' || str[a] == '\n' || str[a] == '\t') {
myWord++;
}
a++;
}
সম্পূর্ণ উদাহরণ
using System;
public class Demo {
public static void Main() {
int a = 0, myWord = 1;
string str = "Hello World!";
while (a <= str.Length - 1) {
if (str[a] == ' ' || str[a] == '\n' || str[a] == '\t') {
myWord++;
}
a++;
}
Console.Write("Number of words in the string = {0}\n", myWord);
}
}
আউটপুট
Number of words in the string = 2
বিকল্প: str.Split(new char[] {' ', '\t', '\n'}, StringSplitOptions.RemoveEmptyEntries).Length ব্যবহার করুন।