![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 3
Rep Power: 0
![]() |
Delimiter Fun
Im writting a program that count words in a text file and the delimiter needs to be any non alphabetic character. Is there a call that will give me this?
Thanks for your help. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
Google is a programmer's best friend. The link below will get you what you need with a few tweaks.
http://www.geocities.com/marcoschmid...ord-count.html btw, welcome to the forum.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2005
Posts: 3
Rep Power: 0
![]() |
Well, my program is significantly more complicated than the one you linked to. While the program didnt mention anything of a non letter delimiter, I do see that the Character class has a isLetter() method. While this would probably work, it would most likely slow my program down a lot and it needs to be very efficient. Is there any better way of doing this?
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You could always write your own:
int count;
for (int i = 0; str[i]; i++) {
if (((str[i] & 0xDF) < 'A') || ((str[i] & 0xDF) > 'Z')) {
count++;
}
count++;EDIT: just realised this was the Java section :o - good luck with the conversion. Unfortunately, I don't know Java, so I can't help with that. Last edited by Ooble; Jan 15th, 2005 at 8:27 AM. |
|
|
|
|
|
#5 |
|
Programmer
|
Here's a Java version for ya, I added some additional checks just to be on the safe side. This method doesn't just look for delimiters, it looks for delimiters preceeded by a letter, that is, it looks for the end of a word. That last IF statement I put in just in case the document ended with a word that had no other characters after it. Oh, and the string parameter should be, of course, the text of the file as a string.
public int countWords(String s) {
int a = -1;
int b = -1;
int count = 0;
for (int i = 0; i < s.length() - 1; i++) {
a = s.charAt(i);
b = s.charAt(i + 1);
if (!(a > 90 || a < 65) || !(a < 97 || a > 122)) {
if ((b > 90 || b < 65) && (b < 97 || b > 122)) {
count++;
}
}
}
if (!(b > 90 || b < 65) || !(b < 97 || b > 122)) {
count++;
}
return count;
}P.S. I didn't do any case switching like Ooble did, but doing so would probably simplify the code a tad, if you felt so inclined. Oh, and I suppose I could have just used the actual characters instead of the ASCII numbers, too. Eh, oh well. Again, that's up to you. ![]() EDIT: I just realized though that I'm not certain what this method will do with line breaks. Just letting you know...
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? Last edited by EdSalamander; Jan 16th, 2005 at 1:08 AM. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jan 2005
Posts: 3
Rep Power: 0
![]() |
Thanks for your help. I just ended up using the isLetter() method of the Character class. I could probably make it more efficent...But ive already spent probably 30 hours on this project. My program sorts and lists the more frequent words. It sorts all the man pages in unix in 1 minute 45 seconds. The demo program we have does it in 10 seconds...hehe
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|