James Bond 007
2012-02-09 13:53:04 UTC
MY CODE:
================================================
#include
#include
#include
#include
#include
using namespace std;
//prototype functions
bool isWhiteSpace(char); //check for white space
char* getToken(char*); //get the token of the input\
//global variables & arrays
string myString; //global string
int i = 0; //global counter
void main()
{
while(1) //infinite loop
{
//prompt user to enter a command
cout << "Command: ";
getline(cin, myString); //100 max characters
//if user enters "quit" anywhere in the string,
//break out of the loop
if(!myString.find("quit"))
{
//error message
cout << "\t***Terminate program***" << endl;
break; //break out of loop
}
else //tokenize the string
{
char* c = myString.c_str();
string token = getToken(c);
cout << token << endl; //print out the tokenize string
}
}
system("pause"); //pause screen
}
//This function will check if there is any white space
//in the string. It will return "true" if there is.
bool isWhiteSpace(char* myString)
{
if(*myString == ' ' || *myString =='\t' || *myString =='\n' ||
*myString =='\v' || *myString =='\f' || *myString =='\r')
return true; //return true if there is a white space
//else if(myString.eof())
// return false; //return false if end of file
else
return false; //return false if there is no white space
}
//This function will move through the input buffer character by
//character. Each invocation will ignore white spaces. When
//it finds the first non-white space it will copy the non-white
//character to a token buffer. When a white space character is
//found, it will put a NULL terminator at the end of the token
//buffer. The token buffer will then be returned to the calling
//function. If the end of the input is reached, the getToken()
//function will return a NULL string.
char* getToken(string s)
{
//const char* c = s.c_str(); //convert the string to char array
char* token = (char*)malloc(sizeof(s.size()) + 1);
//buffer to convert string to char*
strcpy(token, s.c_str()); //copy s (converted) into token
//token[s.size()+1] = 0;
//memcpy(token,s.c_str(),s.size());
//strtok(token, " ");
//NEED HELP HERE!!!!!
//loop until NULL is found
while(token != NULL)
{
//loop through string
for(int a= 0; a <= s.size(); a++)
{
for(int b = 0; b <= a-1; b++)
{
//check for white spaces
if(!isWhiteSpace(token[a]))
{
//if not true, then put NULL at the end of token
token[a] = token[b]; //copy the elements, add NULL char
return token + '\0'; //add null at the end
//token = strtok(NULL, " "); //copy the contents in src into token
//return token;
}
else
{
token[a] = token[a];
return token;
//return token;
}
}
}
i++; //advance to next character
}
}