Solved

Urgent: Functions and Pointers

Posted on 2003-12-11
11
319 Views
Last Modified: 2010-04-02
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
      
}
0
Comment
Question by:CDCOP
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 9920985
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.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9921059
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
0
 
LVL 7

Assisted Solution

by:jj819430
jj819430 earned 150 total points
ID: 9921163
well. You are off to a good start.

Take this one peice at a time and you will get the answer.

Lets try and get you to the next step on each of your problems.

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

    /* ok lets look at how you would do this. Well you start at the beginning of a sentence.
    * Can you assume that a '.','!','?' charachter will be the end of the sentence?
    * That becomes an issue in a sentence such as "I went to Washington D.C. and saw something."
    * I am going to assume that you are allowed to but if not I will try and help you on the other way.
    * So what you have to do is somehow cycle through the charachters until you find one of these charachters.
    * so create a nub function like you had before and build from it.
    *
    *
       void SentenceReader(char * start)
       {
       }
    *  now in here you need to go through and print out your sentence right?
    * so while you loop through looking for the 3 end charachters why not just print the current charachter?


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.

    /* Ok here you have something.
    * What you are trying to do is char something from "hi  I      am good" to "hi I am good"
    * so why not just cycle through the same way as in the problem one. But instead of looking for the ending charachters, look for " ". if that is there
    * then don't add anything until you come to a peice != " ".
 
    char GetOut( char goodstuff, char * nextpiece ){
     //Start Filter
     

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


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).
    /* ok so this is something that can be done in a multitude of ways. The most basic is just to write it out.
    * go through and physically check if the first letter is == to a lowercase letter. If it is then capitalize it.

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).
   
    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.


I hope this gets you to the next part of your project.
0
 
LVL 9

Author Comment

by:CDCOP
ID: 9921187
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.
0
 
LVL 9

Author Comment

by:CDCOP
ID: 9921190
BTW: Thanks
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 11

Expert Comment

by:bcladd
ID: 9921446
(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
0
 
LVL 16

Expert Comment

by:imladris
ID: 9922179
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.
0
 
LVL 9

Author Comment

by:CDCOP
ID: 9922339
I am printing the updates and will go over them in about 15min. I'll post back then. Thanks
0
 
LVL 9

Author Comment

by:CDCOP
ID: 9923233
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.
0
 
LVL 11

Accepted Solution

by:
bcladd earned 350 total points
ID: 9924390
//Word Count
char * WordCount(char * wordsent) {
     int i=0;
     while ( wordsent != NULL ) {
          if( wordsent == " ") {
               i+=1;
               ++wordsent;
          }  else
               ++wordsent;        
     }
     wordsent = i;
     return wordsent;
}

Problem 1: Assignment says to return the number of words. What is the TYPE of number of words? What variable do you store that value in? (Could you come up with a more meaningful name for that variable?)
Problem 2: Assuming wordsent is NOT NULL on entry your while loop will run for a long, long time. You are looking for a null-terminator on a string. That is you are working unitl the character pointed to by your pointer is the null character ('\0'). So, rather than while (wordsent != NULL) you want to work while *wordsent (the character pointed to by wordsent) is not '\0'.
Problem 3: If you are using *wordsent you MUST check that wordsent != NULL BEFORE you dereference it. You should decide what value to return if the pointer is NULL and just return that value without getting to your loop.
Minor Problem 4: Since you increment wordcount in both branches of the if (inside the loop) wouldn't it make more sense to just do that outside of the if statement as the last thing that happens in the loop?

Hope this helps, -bcl
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9924530
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
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now