Write a c# program that calculate the length of string without using anyinbuilt function.

string str = “wordcounter”;
str += ‘\0’;
int x = 0;
while (str[x] != ‘\0’)
x++;
Console.WriteLine(x);

you could write a loop and increment a counter inside this loop:

int numberOfLetters = 0;
foreach (var c in str)
{
numberOfLetters++;
}
// at this stage numberOfLetters will contain the number of letters
// that the string contains

there is also another way:

int numberOfLetters = str.ToCharArray().Length;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s