Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of characters and words in Array

How to count number of characters & words in array in C#?

For e.g.:

char[] arr= "My name is ABC XYZ".Tochararray();

should return 5 as number of words and 18 (space is counted as character) as number of characters.

Thanks!

like image 794
xorpower Avatar asked Jan 20 '23 16:01

xorpower


1 Answers

You can't directly assign a string to an array of integers/chars in c#

string s = "My name is ABC XYZ";

int l = s.Length // 18 chars;
int w = s.Split(' ').Count(); // 5 words 
like image 193
Paolo Falabella Avatar answered Jan 29 '23 04:01

Paolo Falabella