#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Need Help On Advanced Word Separator Program


    Hello,

    So I'm trying to follow an 'advanced' exercise in Chapter 5's exercises section.

    Basically, this program will first take in a paragraph of words separated by spaces, and return the first word. If NULL is passed to this particular function, then it will return the remaining words separated by spaces.

    I have goals at the bottom of my program, and here is my current attempt to just get this running in main first before I try to put the ideas into separate functions.

    Basically what I have so far is a program that checks the current character to see if there's a white space, and if NOT, then it will add the current character to a string accumulator called 'word'. When a white space is found, then the current 'word' is then printed.

    I was also thinking to maybe reset the string accumulator at this point to be blank, but I'm not sure.

    Anyway, I'm just trying to reason out the two big ideas to implement and I have the ideas that I'm thinking about so far at the bottom of my program.

    Here's my code:

    Code:
    /* 
    5. Write a function that, when passed a string consisting of words separated 
    by single spaces, returns the first word; calling it again with an argument 
    of NULL returns the second word, and so on, until the string has been processed 
    completely, when NULL is returned. 
    
    This is a simplified version of the way the native C++ run-time library 
    routine strtok() works. 
    
    So, when passed the string ‘one two three’, the function returns you ‘one’, 
    then ‘two’, and finally ‘three’.
    
    Passing it a new string results in the current string being discarded before 
    the function starts on the new string.
    */
    
    #include <iostream>
    #include <string>
    #include <cctype> // Used to check for spaces later on
    
    using namespace std;
    
    int main()
    {
    	// String that contains main phrase
    	string input;
    
    	// String accumulator that will gather the characters 
    	// of each part of the above phrase
    	string word;
    
    	input = "To Be Or Not To Be";
    
    	cout << "Value of input = " << input << endl;
    	cout << endl;
    
    	// Check to see if the string contains any spaces:
    	
    	for(int i = 0; i < input.length(); i++)
    	{
    		// Print out the current character:
    		cout << "Current Character = " << input[i] << endl;
    
    		// Look For A Character by using the ! Not operator
    		if (!isspace(input.at(i)))
    		{
    			cout << "Not A White Space!" << endl;
    			cout << endl;
    			// Add the current character to word:
    			word = word + input[i];
    
    		}
    
    		else
    		{
    			cout << "White Space Found!" << endl;
    			// Since a white space is found, output the current word
    			// or in the context of the program, return the first word
    			cout << "Word = " << word << endl;
    			cout << endl;
    
    			/* Optional Segment in which I reset the current word instead
    			// of making a continous word
    			// Reset word to be blank:
    			word = "";
    			*/
    		}
    	}
    
    	// First Idea To Implement:
    	// ________________________
    
    	// Write a function that, when passed a string consisting of words separated 
    	// by single spaces, returns the first word;
    
    	// Current Idea On How To Approach This:
    	// Maybe make a string accumulator that will accumulate characters
    	// in a string as long as a character is found, and this phrase will be printed
    	// in the else clause, and then reset to be blank
    
    	// But how do I deal with the second or third word AFTER the first case...
    	// Hmmm...
    
    	// Second Idea To Implement:
    	// ________________________
    
    	// Calling the function again with an argument 
    	// of NULL returns the second word, and so on, until the string has been 
    	// processed completely, when NULL is returned. 
    
    	// Current Idea On How To Approach This:
    	// Maybe an if statement in the function's body itself that will make another
    	// continuous string accumulator
    
    	return 0;
    
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    I'm trying to implement the idea to pass NULL to the main function, but am having trouble reasoning it out.

    Basically, I'm trying to model the situation inside main before I create a function that actually can do this.

    Thing is, in the book itself, it doesn't really discuss how to pass NULL arguments up to this point, and I've looked in that particular chapter, and nothing relates, hence why I'm asking.

    Should I make a boolean argument for this situation? Or should I make the 'word' string accumulator equal to NULL when the first word has been found?

    Sorry if my logic comes off as weird, but I don't know how to deal with NULL arguments at this point. Just trying to do this piece-wise.

    Here's my code at this point, thanks a bunch!:




    Code:
    /* 
    5. Write a function that, when passed a string consisting of words separated 
    by single spaces, returns the first word; calling it again with an argument 
    of NULL returns the second word, and so on, until the string has been processed 
    completely, when NULL is returned. 
    
    This is a simplified version of the way the native C++ run-time library 
    routine strtok() works. 
    
    So, when passed the string ‘one two three’, the function returns you ‘one’, 
    then ‘two’, and finally ‘three’.
    
    Passing it a new string results in the current string being discarded before 
    the function starts on the new string.
    */
    
    #include <iostream>
    #include <string>
    #include <cctype> // Used to check for spaces later on
    
    using namespace std;
    
    int main()
    {
    	// String that contains main phrase
    	string input;
    
    	// String accumulator that will gather the characters 
    	// of each part of the above phrase
    	string word;
    
    	input = "To Be Or Not To Be";
    
    	cout << "Value of input = " << input << endl;
    	cout << endl;
    
    	// Check to see if the string contains any spaces:
    	
    	for(int i = 0; i < input.length(); i++)
    	{
    		// Print out the current character:
    		cout << "Current Character = " << input[i] << endl;
    
    		// Look For A Character by using the ! Not operator
    		if (!isspace(input.at(i)))
    		{
    			cout << "Not A White Space!" << endl;
    			cout << endl;
    			// Add the current character to word:
    			word = word + input[i];
    
    		}
    
    		else
    		{
    			cout << "White Space Found!" << endl;
    			// Since a white space is found, output the current word
    			// or in the context of the program, return the first word
    			cout << "Word = " << word << endl;
    			cout << endl;
    
    			/* Optional Segment in which I reset the current word instead
    			// of making a continous word
    			// Reset word to be blank:
    			word = "";
    			*/
    		}
    
    		// Add an if statement that will check to see if
    		// NULL is passed into the function:
    
    		// Do I check to see if word equals NULL or do I create
    		// a boolean type variable for this purpose?
    
    		if (word == NULL)
    		{
    			cout << "Null has been reached!" << endl;
    
    			// Somehow give the rest of the paragraph aka
    			// second word, third word, fourth word, etc
    		}
    	}
    
    	// First Idea To Implement:
    	// ________________________
    
    	// Write a function that, when passed a string consisting of words separated 
    	// by single spaces, returns the first word;
    
    	// Current Idea On How To Approach This:
    	// Maybe make a string accumulator that will accumulate characters
    	// in a string as long as a character is found, and this phrase will be printed
    	// in the else clause, and then reset to be blank
    
    	// But how do I deal with the second or third word AFTER the first case...
    	// Hmmm...
    
    	// Second Idea To Implement:
    	// ________________________
    
    	// Calling the function again with an argument 
    	// of NULL returns the second word, and so on, until the string has been 
    	// processed completely, when NULL is returned. 
    
    	// Current Idea On How To Approach This:
    	// Maybe an if statement in the function's body itself that will make another
    	// continuous string accumulator
    
    	return 0;
    
    }

IMN logo majestic logo threadwatch logo seochat tools logo