views:

113

answers:

3

Hi all, 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
            {
                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();     

    }        
  }
 } 
A: 

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

Henri
+12  A: 

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.

Eric Lippert
Thanks for the informative answer , but i have a question please , why do you frequently use var instead of string and string[] in decelerations ?
rafael
@rafael: Good question. A full answer would be quite long, but briefly, my reason is that using var further emphasizes the *meaning* of the code over the *mechanism*. What is words? Who cares? It is a collection of words and that's all you need to worry about for the purposes of this algorithm. What is longWords? Who cares? It is the result of filtering "words" and that's all you need to worry about. Does it matter whether it is string[] or List<string> or IEnumerable<string> or IList<string> or something else? No. What matters is its meaning, not the details of its storage.
Eric Lippert
A: 

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);
Femaref