PDA

Click to See Complete Forum and Search --> : Help Required - String Analyser


rob316
Apr 6th, 2005, 06:07 AM
I need to write the following program however im new to programming and i desperately need some help. Its the coding part i struggle with, if someone can start me off, give me tips, help me in any way please reply. The following link is a picture of how this program needs to look.

http://www.wrestlefury.com/prog.jpg

The program should calculate and display the number of words that contain 1, 2, 3, 4 or 5 characters as shown in the example. Any words that contain 6 or more characters should be counted together.

If the user does not enter a string before pressing the button, an error message should be displayed and the user asked to enter another string.

The program should be able to handle situations where there is more than one space between the words in the sentence. Punctuation characters such as comma, full-stop, etc can be treated as characters in words.

Sgt-Peppa
Apr 6th, 2005, 06:52 AM
You dont have anything up till now?
Thats weird!

Well heres a hint for your homework:
Test if your TextBox is empty:


if (null!= textBox1.Text && textBox1.Text !="")


Split your textBox string into a sinlge string array to get each word:


char [] seperator = new char [1];
seperator[0] = Convert.ToChar(" ");
string data = textBox1.Text;
string [] singleWords = data.Split(seperator);


That leaves you with a string array of each seperate word!

Then all you need to do is loop through your array and check the length of your word!


foreach (string tmp in singleWords)
{
string test = tmp.Trim();
switch (test.Length)
{
case 0:
break;
case 1:
oneLetter++;
break;
case 2:
twoLetters++;
break;
case 3:
threeLetters++;
break;
case 4:
fourLetters++;
break;
case 5:
fiveLetters++;
break;
default:
rest++;
break;
}
}


That should get you started! Next time you should start to think about the solution for your self and start to code! Thats the only way to learn! If you have errors or questions about your code, then come back and post here.

Stephan

rob316
Apr 6th, 2005, 07:51 AM
Thanks Steve, i did have some code but it wasnt working for me. Im still having problems now. I put your code in on the analyse button but its not liking the line "char [] seperator = new char [1];" or "string test = tmp.Trim();". im not familiar with these lines especially a tmp.Trim? The errors are "char", ";" and "test". How can i correct these?

Sgt-Peppa
Apr 6th, 2005, 08:29 AM
The trim method delets all heading and trailing whitespaces of a string.

for example
" test "
would Trim to
"test"

Post your code and the error message! I'll have a look at it!

Sgt-Peppa
Apr 7th, 2005, 03:18 AM
I done this code:


string wholeLine;
string [] words;
int wordlength = 0, oneletter = 0,twoletter = 0,threeletter = 0,fourletter = 0,fiveletter = 0,sixormoreletter = 0;
Console.WriteLine("Please enter a sentence\n");
wholeLine = Console.ReadLine();
words = wholeLine.Split(new Char[] {' '});
foreach(string word in words)
{
wordlength = word.Length;
switch(wordlength)
{
case 1:
oneletter++;
break;
case 2:
twoletter++;
break;
case 3:
threeletter++;
break;
case 4:
fourletter++;
break;
case 5:
fiveletter++;
break;
case 6:
sixormoreletter++;
break;
default:
break;
}

}
Console.WriteLine("There are {0} 1 letter words, {1} two letter words, {2} three letter words, {3} four letter words, {4}

five letter words, {5} size or more letter words\n",oneletter,twoletter, threeletter,fourletter,fiveletter,sixormoreletter);
Console.Read();
}

It works in console but i want to convert it so it works with the interface. The textbox is called txtString, the button is called btnAnalyse and the label is called lblResults.


OK put this in your btnAnalise_Click method


if (txtEnterData.Text != "" && null!= txtEnterData.Text)
{

int oneLetter=0;
int twoLetters=0;
int threeLetters=0;
int fourLetters=0;
int fiveLetters=0;
int rest=0;


char [] seperator = new char [1];
seperator[0] = Convert.ToChar(" ");
string data = txtEnterData.Text;
string [] singleWords = data.Split(seperator);

foreach (string tmp in singleWords)
{
string test = tmp.Trim();
switch (test.Length)
{
case 0:
break;
case 1:
oneLetter++;
break;
case 2:
twoLetters++;
break;
case 3:
threeLetters++;
break;
case 4:
fourLetters++;
break;
case 5:
fiveLetters++;
break;
default:
rest++;
break;
}
}
string result = "One Letter occured: " + oneLetter.ToString() + "\r\n";
result = result + "Two Letters occured: " + twoLetters.ToString() + "\r\n";
result = result + "Three Letters occured: " + threeLetters.ToString() + "\r\n";
result = result + "Four Letters occured: " + fourLetters.ToString() + "\r\n";
result = result + "Five Letters occured: " + fiveLetters.ToString() + "\r\n";
result = result + "Rest Letters occured: " + rest.ToString() + "\r\n";
lblResult.Text = result;
}
else
{
MessageBox.Show("Please enter some Text");
}

Replace the txtEnterData with your txtString.

Work from there, I am sure you can find some optimizations for this!