473,232 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

Need to write a function that adjusts words in a string array

I have a program that reads from a file. In the file are a series of
words. I read in all the words into a string array, find the average
length, count the number of words, display the longest word and how long
it is, and display the smallest word and how long it is. All that is
working fine, but what I need to do it format certain words that contain
a period at the end of them (ex: a word at the end of a sentence), words
with quotes around them, exclamation points, etc. If a word has one of
these characters anywhere but the beginning or end it is ok. Also, say
you have this word: one." It needs to be changed to: one

The word: don't is ok because the "'" isn't at the beginning or end.

Jul 22 '05 #1
8 2167
On Mon, 24 Nov 2003 15:23:46 -0800, Rick wrote:
I have a program that reads from a file. In the file are a series of
words. I read in all the words into a string array, find the average
length, count the number of words, display the longest word and how long
it is, and display the smallest word and how long it is. All that is
working fine, but what I need to do it format certain words that contain
a period at the end of them (ex: a word at the end of a sentence), words
with quotes around them, exclamation points, etc. If a word has one of
these characters anywhere but the beginning or end it is ok. Also, say
you have this word: one." It needs to be changed to: one

The word: don't is ok because the "'" isn't at the beginning or end.


That task can easily be accomplished with regular expressions. Look at
Regex++ (http://www.boost.org/libs/regex/index.htm), included in Boost.

Regards,
Henk Burgstra
Jul 22 '05 #2
It needs to be done basically with if/for/while loops.

Henk Burgstra wrote:
On Mon, 24 Nov 2003 15:23:46 -0800, Rick wrote:

I have a program that reads from a file. In the file are a series of
words. I read in all the words into a string array, find the average
length, count the number of words, display the longest word and how long
it is, and display the smallest word and how long it is. All that is
working fine, but what I need to do it format certain words that contain
a period at the end of them (ex: a word at the end of a sentence), words
with quotes around them, exclamation points, etc. If a word has one of
these characters anywhere but the beginning or end it is ok. Also, say
you have this word: one." It needs to be changed to: one

The word: don't is ok because the "'" isn't at the beginning or end.

That task can easily be accomplished with regular expressions. Look at
Regex++ (http://www.boost.org/libs/regex/index.htm), included in Boost.

Regards,
Henk Burgstra


Jul 22 '05 #3
How do I check if tempword[0] equals a '

I get an error when I do tempword[0] == '''

How do I format it different?

void convertarray(string words[], int count)
{
//int firstpos;
int lastpos;
string tempword;
int length;

for(int i = 0; i < count; i++)
{
//firstpos = 0;
tempword = words[i];
lastpos = tempword.length() - 1;
if(tempword[0] == '!' || tempword[0] == '(' || ')' || tempword[0]
== '"' || tempword[0] == '?' ||
tempword[0] == '.' || tempword[0] == ':' || tempword[0] == ';' ||
tempword[0] == ''' ||
tempword[0] == ',')
{
length = tempword.length();
for(int j = 0; j < length - 1; j++)
tempword[j] = tempword[j + 1];
}
}
}

Rick wrote:
I have a program that reads from a file. In the file are a series of
words. I read in all the words into a string array, find the average
length, count the number of words, display the longest word and how long
it is, and display the smallest word and how long it is. All that is
working fine, but what I need to do it format certain words that contain
a period at the end of them (ex: a word at the end of a sentence), words
with quotes around them, exclamation points, etc. If a word has one of
these characters anywhere but the beginning or end it is ok. Also, say
you have this word: one." It needs to be changed to: one

The word: don't is ok because the "'" isn't at the beginning or end.


Jul 22 '05 #4
Ok, this is what I have right now. It's not working quite properly and
I still need to check to see if the first character is a: '

For some reason every word is being converted even though some should
make the if statement false.

void convertarray(string words[], int count)
{
//int firstpos;
int lastpos;
string tempword;
int length;
bool yes;

for(int i = 0; i < count; i++)
{
tempword = words[i];
lastpos = tempword.length() - 1;
if(tempword[0] == '!' || tempword[0] == '(' || ')' || tempword[0]
== '"' || tempword[0] == '?' ||
tempword[0] == '.' || tempword[0] == ':' || tempword[0] == ';' ||
tempword[0] == ',')
{
length = tempword.length();
for(int j = 0; j < length - 1; j++)
tempword[j] = tempword[j + 1];
words[i] = tempword;
}
else
{
words[i] = tempword;
}
}
}

Rick wrote:
How do I check if tempword[0] equals a '

I get an error when I do tempword[0] == '''

How do I format it different?

void convertarray(string words[], int count)
{
//int firstpos;
int lastpos;
string tempword;
int length;

for(int i = 0; i < count; i++)
{
//firstpos = 0;
tempword = words[i];
lastpos = tempword.length() - 1;
if(tempword[0] == '!' || tempword[0] == '(' || ')' || tempword[0]
== '"' || tempword[0] == '?' ||
tempword[0] == '.' || tempword[0] == ':' || tempword[0] == ';' ||
tempword[0] == ''' ||
tempword[0] == ',')
{
length = tempword.length();
for(int j = 0; j < length - 1; j++)
tempword[j] = tempword[j + 1];
}
}
}

Rick wrote:
I have a program that reads from a file. In the file are a series of
words. I read in all the words into a string array, find the average
length, count the number of words, display the longest word and how
long it is, and display the smallest word and how long it is. All
that is working fine, but what I need to do it format certain words
that contain a period at the end of them (ex: a word at the end of a
sentence), words with quotes around them, exclamation points, etc. If
a word has one of these characters anywhere but the beginning or end
it is ok. Also, say you have this word: one." It needs to be
changed to: one

The word: don't is ok because the "'" isn't at the beginning or end.


Jul 22 '05 #5
In article <Mnywb.6324$ML6.1502@fed1read01>,
Rick <rf*****@NOSPAMcox.net> wrote:
How do I check if tempword[0] equals a '

I get an error when I do tempword[0] == '''


Try tempword[0] == '\''

The backslash tells the compiler that you want it to use the second ' as a
literal character, not as a quote mark to delimit another character (its
normal function in a C++ statement).

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #6
I got that, but I'm still not getting the desired output. Here is what
I have so far:

void convertarray(string words[], int count)
{
int lastpos;
string tempword;
int length;
bool yes;

for(int i = 0; i < count; i++)
{
tempword = words[i]; //file?
lastpos = tempword.length() - 1; //4
if(tempword[0] == '!' || tempword[0] == '(' || ')' || tempword[0]
== '"' || tempword[0] == '?' ||
tempword[0] == '.' || tempword[0] == ':' || tempword[0] == ';' ||
tempword[0] == ',' ||
tempword[0] == '\'')
{
cout << "first position matches" << endl;
length = tempword.length();
for(int j = 0; j < length + 1; j++)
tempword[j] = tempword[j];
words[i] = tempword;
}
if(tempword[lastpos] == '!' || tempword[lastpos] == '(' ||
tempword[lastpos] == '"' ||
tempword[lastpos] == '?' || tempword[lastpos] == '.' ||
tempword[lastpos] == ';' ||
tempword[lastpos] == ',' || tempword[lastpos] == '\'')
{
cout << "last position matches" << endl;
length = tempword.length(); //4
for(int k = 0; k < length; k++)
tempword[k] = tempword[k];//file
words[i] = tempword;
}
else
{
cout << "no position matches" << endl;
words[i] = tempword;
}
}
}

Jon Bell wrote:
In article <Mnywb.6324$ML6.1502@fed1read01>,
Rick <rf*****@NOSPAMcox.net> wrote:
How do I check if tempword[0] equals a '

I get an error when I do tempword[0] == '''

Try tempword[0] == '\''

The backslash tells the compiler that you want it to use the second ' as a
literal character, not as a quote mark to delimit another character (its
normal function in a C++ statement).


Jul 22 '05 #7


Rick wrote:

I got that, but I'm still not getting the desired output. Here is what
I have so far:
Devloping debug techniques is something you need to learn.
Just in case you don't have a debugger (which would simplify
things) why not insert some output statements at strategic
positions to enable yourself to watch what the program
is doing and why.

eg.

void convertarray(string words[], int count)
{
int lastpos;
string tempword;
int length;
bool yes;

for(int i = 0; i < count; i++)
{
tempword = words[i]; //file?
lastpos = tempword.length() - 1; //4
cout << "word is " << tempword << endl;
cout << "lastpos: " << lastpos << endl;
if(tempword[0] == '!' || tempword[0] == '(' || ')' || tempword[0]
== '"' || tempword[0] == '?' ||
tempword[0] == '.' || tempword[0] == ':' || tempword[0] == ';' ||
tempword[0] == ',' ||
tempword[0] == '\'')
{
cout << "first position matches" << endl;
length = tempword.length();
cout << "copying first" << length << " characters" << endl;
for(int j = 0; j < length + 1; j++) {
cout << "copy " << tempword[j] << " (" << j << ") to position" << j << endl;
tempword[j] = tempword[j]; }
words[i] = tempword;
cout << "new word is " << tempword << endl;

// note: the above loop does .... nothing
// that means, of course it does something. It copies
// the j-th character to the j-th character. In the
// end tempword hasn't changed :-)
}
if(tempword[lastpos] == '!' || tempword[lastpos] == '(' ||
tempword[lastpos] == '"' ||
tempword[lastpos] == '?' || tempword[lastpos] == '.' ||
tempword[lastpos] == ';' ||
tempword[lastpos] == ',' || tempword[lastpos] == '\'')
{
cout << "last position matches" << endl;
length = tempword.length(); //4
for(int k = 0; k < length; k++)
tempword[k] = tempword[k];//file
// Here again.
// What's this loop got to do?
// it copies the k-th character from tempword to the k-th character
// in tempword. No changes in tempword result from this.
//
words[i] = tempword;
}
else
{
cout << "no position matches" << endl;
words[i] = tempword;
}
}
}


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
What should I use as a debugger? Basically I am just writing it in emacs
and compiling with g++.

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


Rick wrote:

I got that, but I'm still not getting the desired output. Here is what
I have so far:
Devloping debug techniques is something you need to learn.
Just in case you don't have a debugger (which would simplify
things) why not insert some output statements at strategic
positions to enable yourself to watch what the program
is doing and why.

eg.

void convertarray(string words[], int count)
{
int lastpos;
string tempword;
int length;
bool yes;

for(int i = 0; i < count; i++)
{
tempword = words[i]; //file?
lastpos = tempword.length() - 1; //4


cout << "word is " << tempword << endl;
cout << "lastpos: " << lastpos << endl;
if(tempword[0] == '!' || tempword[0] == '(' || ')' || tempword[0]
== '"' || tempword[0] == '?' ||
tempword[0] == '.' || tempword[0] == ':' || tempword[0] == ';' || tempword[0] == ',' ||
tempword[0] == '\'')
{
cout << "first position matches" << endl;
length = tempword.length();


cout << "copying first" << length << " characters" << endl;
for(int j = 0; j < length + 1; j++)

{
cout << "copy " << tempword[j] << " (" << j << ") to position" << j <<

endl;
tempword[j] = tempword[j];

}
words[i] = tempword;


cout << "new word is " << tempword << endl;

// note: the above loop does .... nothing
// that means, of course it does something. It copies
// the j-th character to the j-th character. In the
// end tempword hasn't changed :-)
}
if(tempword[lastpos] == '!' || tempword[lastpos] == '(' ||
tempword[lastpos] == '"' ||
tempword[lastpos] == '?' || tempword[lastpos] == '.' ||
tempword[lastpos] == ';' ||
tempword[lastpos] == ',' || tempword[lastpos] == '\'')
{
cout << "last position matches" << endl;
length = tempword.length(); //4
for(int k = 0; k < length; k++)
tempword[k] = tempword[k];//file


// Here again.
// What's this loop got to do?
// it copies the k-th character from tempword to the k-th character
// in tempword. No changes in tempword result from this.
//
words[i] = tempword;
}
else
{
cout << "no position matches" << endl;
words[i] = tempword;
}
}
}


--
Karl Heinz Buchegger
kb******@gascad.at

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
19
by: ashmangat | last post by:
Hi! now on the chapter "string-class" My assignment is below Word Counter: Write a function that accepts a pointer to a C-String as an argument and returns the number of words contained in the...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
43
by: SLH | last post by:
hi people. im trying to validate input received via a text area on an ASP page before writing it to a database. i cant use client side javascript due to policy, so it all has to happen on the...
3
by: dalearyous | last post by:
ok basically i need to write a program that will replace normal words in a sentence with pirate words. the trick is it needs to be able to take two word phrases. i went about this two different ways:...
2
by: Prime8 | last post by:
I've been researching so much about arrays and matrices and such, but it hasn't helped me at all on my program. Everything is either over my head or doesn't help with the specific program I have to...
2
Dormilich
by: Dormilich | last post by:
Hi, I'm testing my classes for a web page and I stumble upon an error I don't have a clue what it means: Error: Fatal error: Can't use method return value in write context in "output.php" on...
3
by: aashishn86 | last post by:
var weekend = ; var weekendColor = "#e0e0e0"; var fontface = "Verdana"; var fontsize = 1; var gNow = new Date(); var ggWinCal; isNav = (navigator.appName.indexOf("Netscape") != -1) ? true :...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.