开发者

Need help in Hashtable implementation

i'm quite a beginner in C# , i tried to write a program that extract words from an entered string, the user has to enter a minimum length for the word to filter the words output ... my code doesn't look good or intuitive, i used two arrays countStr to store words , countArr to store word length corresponding to each word .. but the problem is i need to use hashtables instead of those two arrays , because both of their sizes are depending on the string length that the user enter , i think that's not too safe for the memory or something ?

here's my humble code , again i'm trying to replace those two arrays with one hashtable , how can this be done ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication2
{
class Program
{

    static void Main(string[] args)
    {
        int i = 0 ;
        int j = 0;
        string myString = "";
        int counter = 0;
        int detCounter = 0;          

        myString = Console.ReadLine();
        string[] countStr = new string[myString.Length];
        int[] countArr = new int[myString.Length];

        Console.Write("Enter minimum word length:");
        detCounter = int.Parse(Console.ReadLine());

        for (i = 0; i < myString.Length; i++)
        {
            if (myString[i] != ' ')
            {
                counter++;
                countStr[j] += myString[i];
            }
            else
            {
 开发者_StackOverflow               countArr[j] = counter;
                counter = 0;
                j++;
            }                                               
        }

        if (i == myString.Length)
        {
            countArr[j] = counter;
        }

        for (i = 0; i < myString.Length ; i++)
        {
           if (detCounter <= countArr[i])
            {
                Console.WriteLine(countStr[i]);
            }   
        }

     Console.ReadLine();     

    }        
  }
 } 


You're not doing too badly for a first attempt but this could be a lot better.

First thing: use TryParse rather than Parse when parsing an integer input by a human. If the human types in "HELLO" instead of an integer, your program will crash if you use Parse; only use Parse when you know that it is an integer.

Next thing: consider using String.Split to split the string into an array of words, and then process the array of words.

Next thing: code like yours that has a whole lot of array mutations is hard to read and understand. Consider characterizing your problem as a query. What are you trying to ask? I'm not sure I completely understand your code but it sounds to me like you are trying to say "take this string of words separated by spaces. Take a minimum length. Give me all the words in that string which are more than the minimum length." Yes?

In that case, write the code that looks like that:

string sentence = whatever;
int minimum = whatever;
var words = sentence.Split(' ');
var longWords = from word in words 
                where word.Length >= minimum 
                select word;
foreach(var longWord in longWords) 
    Console.WriteLine(longWord);

And there you go. Notice how the code reads like what it is doing. Try to write code so that the code conveys the meaning of the code, not the mechanism of the code.


One word. Dictioary (or HashTable). Both are standard datatypes you can use


Use a Dictionary for this (in your case, you are looking for a Dictionary).

Your extracted string will be the key, the length of it the Value.

Dictionary<string, int> words = new Dictionary<string,int>();
//algorithm
words.Add(word, length);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜