![]() |
| ||
| Re: Determining the number of unique words in a .txt file >I tried a different Approach to achieve the same It's not only "different" approach: it's a wrong approach ;). The C Standard (7.4.1.10): The standard white-space characters are the following:Therefore the code above can't select word in "123four5six..." string. Also it has obviously incorrect lines, for example: while (ptrFirst) { // probably, must be *ptrFirstMore subtle defect of the code above is that isXXX family function do not work for negative arguments. If a character in a text file has a bit value '1xxxxxxx' and implementation char type is signed then [icode]*ptrIter[/code] expression gets negative integer and [icode]isalpha(*ptrIter)[/code] result is undefined. That's why Uchar typedef was defined in my code.In actual fact it's inaccurate implementation of the same (scanner-like) approach ;) Apropos, if we have text file with whitespace separators only, no need in scanner-like methods at all. The simplest code works fine: string word; |
| ||
| Re: Determining the number of unique words in a .txt file My Bad with the Line while (ptrFirst) { Agreed with C99 standard, But the requirement doesn't say anything regarding the numeric separated words. thats why I've implemented the code this way, again your fstream approach is simplest but the thing is that I've tried to use the char* instead of streams provided function.Thanks. by the way I didn't compile it. |
| ||
| Re: Determining the number of unique words in a .txt file Strictly speaking, there is 1 word per line in those strange requirements (linear search only, dont use string, cant use for loop etc - it's enough to make you weep). If so no need in word extraction code at all. Well, if you don't like "stream-based approach", don't use C++ fstream to get lines from a file. Use fgets or what else from C stuff. Furthemore, it's so easy to adopt the code for C-string scan: change f.get(c) to the next char extraction code with null byte test. Oh, sorry, I forgot: don't use istringstream! Don't use C++ at all ;)... |
| ||
| Re: Determining the number of unique words in a .txt file ArkM: I am not denying your opinions but the thing is that we should start learning from basics and Programming language has nothing to do with the logic, if you understand the logic then I suggest to use the built-in functions otherwise creating a raw logic is always a good starting point. |
| ||
| Re: Determining the number of unique words in a .txt file Is it programming learning basics: don't use for loops, don't use this, don't use that... and so on? It's a profanation. Better download and read well-known B.Stroustrup's article "Learning Standard C++ as a New Language": http://www.research.att.com/~bs/new_learning.pdf |
| ||
| Re: Determining the number of unique words in a .txt file Thanks ArkM I've gone through this article of Bjarne, no contradiction with this document at all, but as an experienced programmer what do you think of requirements, practically speaking "One day your boss come to your desk and ask I've bought a library written in C and I want you to use that for blah blah?" or what if your boss ask you to develop a C library itself ? I am not telling you that C is superior than C++, but the thing that matter is requirements, if someone asks for C code teach them C but also provide them with the C++ implementation and the differences between the two. I think this is better learning approach. |
| ||
| Re: Determining the number of unique words in a .txt file Thanks for all the input. I was able to store the lines with strcpy(). but now I'm trying to use strncmp to find out the number of unique words (or lines) in the text file. I tried a couple of things, but none seemed to work. I'm given these guideline- You must use the linear search algorithm to determine if a word is in the array. Remember that the array is an array of structures and that the key is a string (char array) so the string comparison must be used. The search task should be a separate function. The search must be a separate function that returns an integer values. Do not use a for loop and the function must have only one return statement. Heres the instructors linear search- int search (int list [], int size, int key)I'm really having trouble on this, any help would be appreciated. |
| ||
| Re: Determining the number of unique words in a .txt file bool search(char ** words, int numWords, char * currentWord) |
| ||
| Re: Determining the number of unique words in a .txt file Quote:
Now let's remember: this is the C++ language thread and we are talking about C++ here. |
| ||
| Re: Determining the number of unique words in a .txt file Ok, heres my revised code- #include <iostream>My search functions still arent giving me the results I want, it just returns the number of words, not unique words. |
| All times are GMT -4. The time now is 1:05 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC