turn the tokens array into an array of c strings (In other words, in the format
ID: 3754832 • Letter: T
Question
turn the tokens array into an array of c strings (In other words, in the format of char **) using emacs.
using
char ** ConvertToC (string tokens [], int tokenCount);
void CleanUpCArray (char ** cTokens, int tokenCount);
void Printreverse (char ** cTokens, int tokenCount);
a sample input would be
input:This is a test
output:test a is This
input:to see what happens
output: happens what see to
main.cpp
___________________________________________________________
#include "HALmod.h"
int main()
{
string tokens [MAX_COMMAND_LINE_ARGUMENTS];
int tokenCount;
char ** cTokens;
do
{
tokenCount = GetCommand (tokens);
// cTokens= ConvertToC ( tokens, tokenCount);
ProcessCommand (tokens, tokenCount);
Printreverse (cTokens, tokenCount);
CleanUpCArray (cTokens, tokenCount);
} while (1);
return 0;
}
HALmod.h
___________________________________________
#include
#include
#include
#include
#include
#include
using namespace std;
//The following two lines come from HALglobals.h
const int MAX_COMMAND_LINE_ARGUMENTS = 8;
const int SLEEP_DELAY = 100000;
int GetCommand (string tokens []);
int TokenizeCommandLine (string tokens [], string commandLine);
bool CheckForCommand ();
void ProcessCommand (string tokens [], int tokenCount);
//void ShutdownAndRestart (string tokens [], int tokenCount);
static volatile sig_atomic_t cullProcess = 0;
// new stuff
/*
char ** ConvertToC (string tokens [], int tokenCount);
void CleanUpCArray (char ** cTokens, int tokenCount);
void Printreverse (char ** cTokens, int tokenCount);
*/
HALmod.cpp
________________________________________________
#include "HALmod.h"
int GetCommand (string tokens [])
{
string commandLine;
bool commandEntered;
int tokenCount;
do
{
cout << "HALshell> ";
while (1)
{
getline (cin, commandLine);
commandEntered = CheckForCommand ();
if (commandEntered)
{
break;
}
}
} while (commandLine.length () == 0);
tokenCount = TokenizeCommandLine (tokens, commandLine);
return tokenCount;
}
int TokenizeCommandLine (string tokens [], string commandLine)
{
char *token [MAX_COMMAND_LINE_ARGUMENTS];
char *workCommandLine = new char [commandLine.length () + 1];
int i;
int tokenCount;
for (i = 0; i < MAX_COMMAND_LINE_ARGUMENTS; i ++)
{
tokens [i] = "";
}
strcpy (workCommandLine, commandLine.c_str ());
i = 0;
if ((token [i] = strtok (workCommandLine, " ")) != NULL)
{
i ++;
while ((token [i] = strtok (NULL, " ")) != NULL)
{
i ++;
}
}
tokenCount = i;
for (i = 0; i < tokenCount; i ++)
{
tokens [i] = token [i];
}
delete [] workCommandLine;
return tokenCount;
}
//Do not touch the below function
bool CheckForCommand ()
{
if (cullProcess)
{
cullProcess = 0;
cin.clear ();
cout << " ";
return false;
}
return true;
}
//This is a very paired down version of Dr. Hilderman's function
void ProcessCommand (string tokens [], int tokenCount)
{
if (tokens [0] == "shutdown" || tokens [0] == "restart" || tokens [0] == "lo")
{
if (tokenCount > 1)
{
cout << "HALshell: " << tokens [0] << " does not require any arguments" << endl;
return;
}
cout << endl;
cout << "HALshell: terminating ..." << endl;
exit (0);
}
}
/*
char ** ConvertToC (string tokens [], int tokenCount);
{
char ** cstring;
cstring = new char *[8];
return cstring;
}
void CleanUpCArray (char ** cTokens, int tokenCount)
{
for (int i=0; i<8; i++)
{
free(cTokens[i]);
}
}
void Printreverse (char ** cTokens, int tokenCount)
{
for (int i= tokenCount; i>=0; i--)
{
cout << cTokens[i];
}
}
char ** ConvertToC(string tokens[], int tokenCount)
{
return nullptr;
}
*/
Explanation / Answer
If you have any doubts, please give me comment...
HALmode.h
#include<iostream>
#include<string>
#include<csignal>
#include<cstring>
#include<cstdlib>
// #include
using namespace std;
//The following two lines come from HALglobals.h
const int MAX_COMMAND_LINE_ARGUMENTS = 8;
const int SLEEP_DELAY = 100000;
int GetCommand (string tokens []);
int TokenizeCommandLine (string tokens [], string commandLine);
bool CheckForCommand ();
void ProcessCommand (string tokens [], int tokenCount);
//void ShutdownAndRestart (string tokens [], int tokenCount);
static volatile sig_atomic_t cullProcess = 0;
// new stuff
char ** ConvertToC (string tokens [], int tokenCount);
void CleanUpCArray (char ** cTokens, int tokenCount);
void Printreverse (char ** cTokens, int tokenCount);
HALmod.cpp
#include "HALmod.h"
int GetCommand(string tokens[])
{
string commandLine;
bool commandEntered;
int tokenCount;
do
{
cout << "HALshell> ";
while (1)
{
getline(cin, commandLine);
commandEntered = CheckForCommand();
if (commandEntered)
{
break;
}
}
} while (commandLine.length() == 0);
tokenCount = TokenizeCommandLine(tokens, commandLine);
return tokenCount;
}
int TokenizeCommandLine(string tokens[], string commandLine)
{
char *token[MAX_COMMAND_LINE_ARGUMENTS];
char *workCommandLine = new char[commandLine.length() + 1];
int i;
int tokenCount;
for (i = 0; i < MAX_COMMAND_LINE_ARGUMENTS; i++)
{
tokens[i] = "";
}
strcpy(workCommandLine, commandLine.c_str());
i = 0;
if ((token[i] = strtok(workCommandLine, " ")) != NULL)
{
i++;
while ((token[i] = strtok(NULL, " ")) != NULL)
{
i++;
}
}
tokenCount = i;
for (i = 0; i < tokenCount; i++)
{
tokens[i] = token[i];
}
delete[] workCommandLine;
return tokenCount;
}
//Do not touch the below function
bool CheckForCommand()
{
if (cullProcess)
{
cullProcess = 0;
cin.clear();
cout << " ";
return false;
}
return true;
}
//This is a very paired down version of Dr. Hilderman's function
void ProcessCommand(string tokens[], int tokenCount)
{
if (tokens[0] == "shutdown" || tokens[0] == "restart" || tokens[0] == "lo")
{
if (tokenCount > 1)
{
cout << "HALshell: " << tokens[0] << " does not require any arguments" << endl;
return;
}
cout << endl;
cout << "HALshell: terminating ..." << endl;
exit(0);
}
}
char **ConvertToC(string tokens[], int tokenCount)
{
char **cstring;
cstring = new char *[8];
for(int i=0; i<tokenCount; i++){
cstring[i] = new char[tokenCount];
strcpy(cstring[i], tokens[i].c_str());
}
return cstring;
}
void CleanUpCArray(char **cTokens, int tokenCount)
{
for (int i = 0; i < 8; i++)
{
delete[] cTokens[i];
}
}
void Printreverse(char **cTokens, int tokenCount)
{
for (int i = tokenCount-1; i >= 0; i--)
{
cout << cTokens[i];
if(i!=0)
cout<<" ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.