Forum Settings
       
Reply To Thread

I need a C++ string class.Follow

#1 Oct 06 2004 at 10:46 PM Rating: Good
If anyone has one kicking around on their hard drive that they don't mind sharing, I could really use it. It's been a while since I've done any coding, but I have a little project I want to tackle.

If you do, and feel like sharing it, could you mail it to pellaeon99@aol.com?
#2 Oct 07 2004 at 2:06 PM Rating: Good
***
3,771 posts
I remember the Standard Template Library string class was always enough for what I wanted to do. Are you looking for some special functionality?

What's your little project? I'm curious.
#3 Oct 08 2004 at 1:10 AM Rating: Good
Just something that reads a text file, sorts it out by word, then sorts the words by frequency. I want to run the debate transcripts through it and see what he various keywords are and such.
#4 Oct 08 2004 at 12:59 PM Rating: Good
***
3,771 posts
If you are just looking to manipulate some strings, C++ might not be the best choice of languages.

I recently got into PERL, which was originally built to focus on I/O and string manipulation. You should look into it if you have little projects like this often.

It's quick, easy, portable, not incredibly powerful, but very useful for common tasks. For example, I wrote this pretty quickly (in half my lunch break).

wordcount.pl wrote:
##### open the file and read the contents into one big string
open (FILEHANDLE, @ARGV[0]);
for ($line_number=1;<FILEHANDLE>;$line_number++)
{$contents = $contents . $_;}
close (FILEHANDLE);



##### remove punctuation and extra whitespace
$contents =~ s/\W+/ /g;

##### parse each word into its own array element
@word_list = split / /, $contents;

##### sort the list
@word_list = sort @word_list;


##### count the repeated elements
foreach $word (@word_list)
{
$size_of_results = @results;
if (@results[$size_of_results-1] eq $word & $size_of_results >0)
{
@count[$size_of_results-1] ++;
}
else
{
@results[$size_of_results] = $word;
@count[$size_of_results] = 1;
}
}


##### print the results
for ($c=0;$c<=$size_of_results;$c++)
{
print "@count[$c] : @results[$c]\n";
}



This does about what you want to do (I think).
If you want to install perl and run it from the command line like this

>perl wordcount.pl transcriptfile.txt

let me know how it works out.

Edited, Fri Oct 8 14:00:18 2004 by highRfrequenC
#5 Oct 09 2004 at 9:57 AM Rating: Good
Hehe, I'll try it out later, when I have time. If you read this between now and then. Could you give me a line for it to print to file?

Edited, Sat Oct 9 12:53:54 2004 by Lubriderm
#6 Oct 09 2004 at 8:57 PM Rating: Good
I'm going to need a line for converting all of the capital letters into lowercase also. I just picked up a perl book, but I'm sure that I wont get to anything that complicated in time.

Any help would be appreciated.
#7 Oct 09 2004 at 9:11 PM Rating: Good
Here's what doesn't work:

[li] I wanted to sort the list by frequency after all was said and done.
[li] It will not handle contractions such as can't and won't (come out as "can" "won" and 2: "t" if I were to use them both).
[li] Does not handle capital letters. (Fish would be a different word than fish).

Well, whether I ever get this done or not, thank you very much for turning me on to perl. It seems pretty fun (in a geeky kinda way [i'm a geek]).
#9 Oct 10 2004 at 1:36 AM Rating: Good
Yeah, I'll just keep plugging through the book. Perl is kinda nice because it is a relatively high lvl language with low lvl functionality. Sure there are tradeoffs, but it seems great for text manipulations.
#10 Oct 10 2004 at 9:05 AM Rating: Good
***
3,771 posts
To print to a file is easy.

open FILEHANDLE, ">$filename";
print FILEHANDLE "some stuff to write";
close FILEHANDLE;


my favorite feature of perl, is its use of regular expressions. the =~ operator binds a string to an expression, such as a substitution s///


this line substitutes everything that is not a letter, number or underscore with blank space:
$contents =~ s/\W+/ /g;

the s/ makes it a substitution, the /g makes it apply to every match it finds instead of just the first, and the \W+ is the match expression where \W is the same as [A-Za-Z0-9_] and the plus means there are many instances

so to include apostrophes in our match expression we can change this line to :
$contents =~ s/[A-Za-z0-9_']+/ /g;


You can also convert upper to lower case in one line with a regular expression.

If you have a book in front of you (I dont right now) there is likely an example you can copy directly.
#11 Oct 10 2004 at 7:40 PM Rating: Good
Oh, and I just wanted to say thank you very much again. All of you. ^_^
Reply To Thread

Colors Smileys Quote OriginalQuote Checked Help

 

Recent Visitors: 26 All times are in CST
Anonymous Guests (26)