Link to home
Start Free TrialLog in
Avatar of CDCOP
CDCOP

asked on

Urgent: Functions and Pointers

TIA!!!

1) A function that takes a pointer to a character (the beginning of a
    sentence) and returns the number of words in the sentence

2) A function that takes a pointer to a character (the beginning of a
    sentence) and takes out any extraneous spaces in the sentence
    (there should be no spaces at the beginning of the sentence and
    there should be exactly one space between words)
    Possible strategy: place the "good" characters from the original
    sentence into another string, then copy the fixed string over the
    original.

3) A function that takes a pointer to a character (the beginning of a
    sentence) and capitalizes the first word in the sentence (if it is
    a lower case letter).

4) A function that takes a pointer to a character (the beginning of a
    sentence) and places a period or question mark on the end of the
    sentence if necessary. (Sentences that begin with "Who", "What",
    "When", "Where", "Why", "How"... should end with a question mark).

Each of the above sets of actions must take place in separate
functions.


Here is my Code so Far:
#include<iostream>

using std::cin;
using std::cout;
using std::endl;

//Capitalize the First word in the sentence
char * SentCap( char * ptr );

//Filter out the EvIL
char GetOut( char goodstuff, char * nextpiece );

int main()
{
      // Start Variable Declarations
      char sentence[101];
      char s1[101];
      char s2[101];
      char goodstuff[101]="";
      char * nextpiece;
      // End Variable Declarations
      
      //Get user input
      cout<<"Enter a sentence: ";
      cin.getline (sentence, 101);
      
      //cout<< GetOut( *nextpiece, goodstuff );
      //Start GOOD
      
      /*char * ptr = sentence;
      while ( *ptr != ' ' && *ptr != '\0' )
            ++ptr;
      ++ptr;
      cout << ptr << endl;
      //cout<< SentCap( ptr );
      
        
      //Caps for first word
      
      */
      //End GOOD
      nextpiece = strtok( sentence, "^&#$" );
      cout<<GetOut(goodstuff[101], nextpiece);

      

      return 0;
}


//Capitalize the First word in the sentence
char * SentCap( char * ptr ){
      
      * ptr = toupper( * ptr);
      
      return ptr;

}


//Filter out the EvIL
char GetOut( char goodstuff, char * nextpiece ){
      //Start Filter
      

      while ( nextpiece != NULL )
      {
            strcat( goodstuff, nextpiece );
            nextpiece = strtok(NULL, "#$^&");
      }
      
      return goodstuff[101];
      //END Filter
      
}
Avatar of imladris
imladris
Flag of Canada image

We're happy to help with homework; but we can't do it for you. A generic request for help/hints is rather too broad. One could write pages without helping unless we know what your problems are. Please ask a more specific question.
Avatar of bcladd
bcladd

You could help us (and your grade) by including header comments on the functions that explain what the parameters are, how they are used, and what the return value is supposed to be.

One thing off the bat: I am guessing that you want goodstuff (as passed into GetOut) to be a pointer at char and you probably want to return a pointer at a char as well. Right now you are just passing the last character in the main() goodstuff (and strcat probably won't compile with that value) and then you try to treat the character as an array.

Hope this helps, -bcl
SOLUTION
Avatar of jj819430
jj819430

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CDCOP

ASKER

GetOut is supposed to take the goodstuff array and the pointer. Yes it should display all of goodstuff instead of the last char. No it won't complie.

The SentCap function works properly. I commented it out so i could try to get the GetOut function working.

I just can't get the data in GetOut to return the right data, I don't know if it is how i'm passing it or not. That's what I assume because it will work find if i just write the code for it in the main, with out function crap (and make a few changes because it's not a function anymore).

From #1. I also don't know how to count and determine how many words are in the sentence, and what data should be passed to the function. I think it would be fairly simple like the SentCap.

From #4. I also can figure out how I need to search for a word like "When" in a sentence (if statement?) and then move the pointer to the end and place a question mark on it. (else a ".")

Let me know if I should be more specific about a part. I know i'm somewhat vague.
Avatar of CDCOP

ASKER

BTW: Thanks
(1) GetOut is returning a single character. From what I read it is supposed to return the fixed up version of the sentence (with the evil characters removed). A sentence is a char *.
You are going to build the sentence inside of goodstuff. goodstuff is a sentence. That means you should pass in and return a char * rather than a char.

You should just return goodstuff, not goodstuff[101] since inside of GetOut goodstuff is just a pointer at some characters.

Hope this helps, -bcl
For counting the number of words in a string you can scan through the string checking each character. Each time you encounter a space increment your wordcount. Add one more at the end to compensate for the fact that the last word won't have a space after it.

For checking what a sentence starts with use strncmp.
Avatar of CDCOP

ASKER

I am printing the updates and will go over them in about 15min. I'll post back then. Thanks
Avatar of CDCOP

ASKER

Ok. jj819430 thanks for your help, you will get points so will bcladd and maybe someone else. I just have a few more questions and then I will determine how to split the points (if at all). Ok. here is the revised and cleaned up code:

#include<iostream>

using std::cin;
using std::cout;
using std::endl;

//Count words
char * WordCount(char * wordsent);

//Capitalize the First word in the sentence
char * SentCap( char * ptr );

//Filter out the EvIL (Bad char.)
char * GetOut( char * goodstuff, char * nextpiece );

int main()
{
      // Start Variable Declarations
      char sentence[101];
      //      char s1[101]; Not used yet
      //      char s2[101]; Not used yet
      char goodstuff[101]="";
//      char * nextpiece;
      
      // End Variable Declarations
      
      //Get user input
      cout<<"Enter a sentence: ";
      cin.getline (sentence, 101);
      

      
      /*
      //Start Caps for first word *Working*
      char * ptr = sentence;
      cout<< SentCap( ptr );
      //End Caps for first word
      */
      
      /*
      //Start Filter out bad char *Working*
      nextpiece = strtok( sentence, "^&#$" );
      cout<<GetOut(goodstuff, nextpiece)<<endl;
      //End Filter out bad char
      */
      
      //Start WordCount
      
      char * wordsent = sentence;
      cout<<WordCount( wordsent );
      return 0;
}

//Word Count
char * WordCount(char * wordsent){
      
      int i=0;

      while ( wordsent != NULL )
      {
            if( wordsent == " ")
            {
                  i+=1;
                  ++wordsent;
            }
            else
                  ++wordsent;
            
      }
      wordsent = i;
      return wordsent;
}


//Capitalize the First word in the sentence
char * SentCap( char * ptr ){
      
      * ptr = toupper( * ptr);
      
      return ptr;
}


//Filter out the EvIL
char * GetOut( char * goodstuff, char * nextpiece ){
      
      while ( nextpiece != NULL )
      {
            strcat( goodstuff, nextpiece );
            nextpiece = strtok(NULL, "#$^&");
      }
      
      return goodstuff;
}


I am having trouble on WordCount moving through the sentence to find the spaces, and then returning the number of words.

jj819430, You said "Again this is a set of conditions. If this is true then do this.
    So set a boolean if true means the sentence should end with a question mark.
    If false end with a period."

boolean? I don't know what that is. Can you be more specific on your answer?
I don't know how to end the sentence if it finds the specified words.

Thanks for your help guys.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Boolean: A variable that can take on only one of two values, true or false. Named for an English mathematician/logician named George Boole.

You have actually been using Boolean expressions (perhaps without knowing the name) whenever you use an if, while, or even a for statement:

if (x > 10) {
  // greater
} else {
  // not greater
}

x is either greater than 10 (and (x > 10) is true) or it is not (and (x > 10) is false). Thus (x > 10) is a Boolean expression. It is useful to be able to store such values in variables so C++ has a "bool" type (like int or char) that can hold a boolean value. You could write a function that returned true if there were an 'A' in the sentence it was passed and false otherwise. Note that you can't just check the first character: you have to check them all and keep track of what you saw.

bool HasAnA(char * sentence) {
  if (sentence == NULL)
    return false; // false is a built in constant that is a false boolean value
  bool seenAnA = false; // we have not yet seen an A
 
  while (*sentence != '\0') {
    if (*sentence == 'A')
      seenAnA = true; // true is a built in value representing true
  }
  // here seenAnA is either true or false. We can print a little message and return the value
  if (seenAnA) {
    cout << "There was an A" << endl;
  else
    cout << "No A seen...how sad." << endl;
 
  return seenAnA;
} // HasAnA

Hope this helps, -bcl