hi, I wrote a program to input senteces, to analyze them and to give me the numer of words in the sentece, the program is running and compiling, but after I put in the sentence and get the number of words from the program I also get "out of bound" and a general error, can anybody take a look,
thanks in advance Mike.
using
System;using
System.Collections.Generic;using
System.Text;namespace sentence
{
class WordCount
{
static void Main(string[] args){
string sentence; string[] wordList; string word; int[] sizeCount = new int[6]; int i; int characterCount = 0; string runAgain; bool continueRunning = true; Console.WriteLine("The program will analyze sentences you type in."); while (continueRunning == true){
for (i = 0; i <= 5; i++){
sizeCount[i] = 0;}
Console.WriteLine(); Console.Write("Please enter a sentence: ");sentence =
Console.ReadLine(); while (sentence.Length == 0){
Console.Write("You must enter some text. Please try again: ");sentence =
Console.ReadLine();}
Console.WriteLine();wordList = sentence.Split(
new char[] { ' ' }); Console.WriteLine("The number of words in the sentence is " + wordList.Length); for (i = 0; i <= wordList.Length; i++){
word = wordList[i].Trim();
if (word.Length > 0){
if (word.Length > 5){
sizeCount[0]++;
}
else{
sizeCount[word.Length]++;
}characterCount = characterCount + word.Length;
}
}
Console.WriteLine("The number of words with 1 letter is " + sizeCount[1]); for (i = 1; i <= 5; i++){
Console.WriteLine("The number of words with " + i + " letters is " + sizeCount[i]);}
Console.WriteLine("The number of words with 6 or more letters is " + sizeCount[0]); Console.WriteLine("The number of non-white space characters is " + characterCount); Console.WriteLine(); Console.Write("Do you want to analyse another sentence (Y/N)? ");runAgain =
Console.ReadLine().ToUpper(); while (runAgain != "Y" && runAgain != "N"){
Console.Write("Please enter Y or N: ");runAgain =
Console.ReadLine().ToUpper();}
if (runAgain == "Y"){
continueRunning =
false;}
}
}
}
}
mike HaninPosted Jan 30, 2008, 5:40 AM
AlanPosted Jan 30, 2008, 5:19 AM
The problem is this line:
for (i = 0; i <= wordList.Length; i++)
which should be:
for (i = 0; i < wordList.Length; i++)
because the last element of wordList is wordList[wordList.Length -1].