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

Discussion in 'C++' started by Rick, Nov 24, 2003.

  1. Rick

    Rick Guest

    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.
     
    Rick, Nov 24, 2003
    #1
    1. Advertising

  2. 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
     
    Henk Burgstra, Nov 24, 2003
    #2
    1. Advertising

  3. Rick

    Rick Guest

    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
     
    Rick, Nov 25, 2003
    #3
  4. Rick

    Rick Guest

    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;
    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.
    >
     
    Rick, Nov 25, 2003
    #4
  5. Rick

    Rick Guest

    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;
    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 = tempword;
    }
    else
    {
    words = 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;
    > 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.
    >>

    >
     
    Rick, Nov 25, 2003
    #5
  6. Rick

    Jon Bell Guest

    In article <Mnywb.6324$ML6.1502@fed1read01>,
    Rick <> 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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Nov 25, 2003
    #6
  7. Rick

    Rick Guest

    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; //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 = 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 = tempword;
    }
    else
    {
    cout << "no position matches" << endl;
    words = tempword;
    }
    }
    }

    Jon Bell wrote:
    > In article <Mnywb.6324$ML6.1502@fed1read01>,
    > Rick <> 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).
    >
     
    Rick, Nov 25, 2003
    #7
  8. 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; //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 = 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 = tempword;
    > }
    > else
    > {
    > cout << "no position matches" << endl;
    > words = tempword;
    > }
    > }
    > }


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 25, 2003
    #8
  9. Rick

    Rick Guest

    What should I use as a debugger? Basically I am just writing it in emacs
    and compiling with g++.

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    >
    >
    > 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; //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 = 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 = tempword;
    > > }
    > > else
    > > {
    > > cout << "no position matches" << endl;
    > > words = tempword;
    > > }
    > > }
    > > }

    >
    > --
    > Karl Heinz Buchegger
    >
     
    Rick, Nov 25, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Strøiman
    Replies:
    1
    Views:
    2,118
    Peter Strøiman
    Aug 23, 2005
  2. BerlinBrown
    Replies:
    6
    Views:
    4,777
  3. TR
    Replies:
    0
    Views:
    282
  4. pantagruel
    Replies:
    8
    Views:
    474
    Dr John Stockton
    Jul 22, 2006
  5. edfialk
    Replies:
    2
    Views:
    99
    edfialk
    Aug 3, 2007
Loading...

Share This Page