Question
Project Description: c progamming only please In this project, you will design and implement a program to perform any of the following commands on strings.
Project Description: c progamming only please
In this project, you will design and implement a program to perform any of the following commands on strings. The command is chosen by typing in the string in square brackets at the input prompt.
[new] New String: Prompts the user for a string and stores it as the current string. Initially the current string is empty.
[list] List: Prints the current string to the screen.
[append] Append: Prompts the user for a string and adds this string to the end of the current string.
[find] Find: Prompts the user for a string and checks to see if it exists as a sub-word in the current string. If so, prints out the number of times the string entered exists as a sub string.
[replace] Replace: Replace the current string with the output of the last string modification command.
[toggle] Toggle: Prints the current string by toggling the case, i.e., Convert upper case letter to lower case letter and vice versa.
[rev] Reverse: Print the current string in the reverse order, character by character.
[wrev] Word Reverse: Print the current string in the reverse order, word by word.
[stat] Statistics: Prints the statistics of the current string in terms of the length of the string, the number of words, the frequency of each letter of the alphabet, etc.
[help] Help: List the different commands and their descriptions.
[hist] History: Prints a list of the past N commands to screen.
[quit] Quit: Quits the program.
[palindrome] Palindrome: [bonus points: not required] determines whether the current string is a palindrome. If so, prints the current string in quotes followed by the message is a palindrome!
Project Design:
You program will print a welcome message, and then display a prompt cmd> and wait for the user to enter a command. If the command is an empty command, i.e. the user presses the enter key, you need to display the prompt again on the next line. If the command is invalid, you need to display an error message.
In the beginning, the current string is empty. To start playing with strings, you need to use the new command to store a new string into the current string.
New
The new command is used to take from the user a new string to play with.
cmd> new
Enter new string: This is my first string
List
The list command will print out the current string to the screen. If the current string is empty, nothing will be displayed.
cmd> list
This is my first string
Append
The append command takes an argument string and appends it to the end of the current string. The appended string and the old current string are separated by a space. It changes the current string.
cmd> new
Enter new string: This is
cmd> append
Enter string to append: my first string
cmd> list
This is my first string
Before appending a string to the current string, you need to make sure that the length of the new string does not exceed the maximum length of the string (MAX_STR_LEN). If exceeded, an error message should be printed. (No need to provide the testing result on this, but should be considered in the program.)
Find
The find command takes an argument string and checks to see if it occurs as a substring in the current string. If then prints out the number of times it exists in the current string. The find command is case sensitive.
cmd> list
This is my first string
cmd> find
Enter sub string to find: is
The substring exists 2 times in the current string.
cmd> find
Enter sub string to find: w
The substring exists 0 times in the current string.
Toggle
The toggle command toggles the case of the current string and prints it to the screen. It doesnt change the current string.
cmd> toggle
tHIS IS MY FIRST STRING
cmd> list
This is my first string
Replace
The replace command takes the resulting string of the last command and copies it into the current string.
cmd> toggle
tHIS IS MY FIRST STRING
cmd> replace
cmd> list
tHIS IS MY FIRST STRING
cmd> toggle
This is my first string
cmd> list
tHIS IS MY FIRST STRING
Reverse
The reverse command prints the current string in reverse order character by character. It doesnt change the current string.
cmd> rev
gnirts tsrif ym si sihT
Statistics
The statistics command prints the statistics of the current string in terms of its length in characters, in words, and the frequency of the different characters in the string.
cmd> stat
Satistics:
length: 23
word: 5
Frequency:
frequency of f: 1
frequency of g: 1
frequency of h: 1
frequency of i: 4
frequency of m: 1
frequency of n: 1
frequency of r: 2
frequency of s: 4
frequency of t: 2
frequency of y: 1
frequency of T: 1
Word Reverse
The reverse command prints the current string in reverse order word by word. It doesnt change the current string.
cmd> wrev
string first my is This
Help
The help command prints a list of commands to the screen.
cmd> help
[new] Enter New String
[list] List Current String
[append] Append a String to the End of Current String
[find] Find a String in Current String
[replace] Replace Current String with Output of Last Command
[toggle] Toggle the Case of Current String
[rev] Print Current String in Reverse Order Character By Character
[wrev] Print Current String in Reverse Order Word By Word
[stat] Print Statistics of Current String
[help] Print This Help Screen
[hist] Print a History of Commands Entered
[quit] Quit Program
[palindrome] Check palindrome
History
The history command prints to the screen the last HISTORY_SIZE (defined as a preprocessor constant) commands entered by the user. Each time when a command is entered, you should update your history and the current history table size. If the history is full, you should discard the oldest history and insert the newest command into the history. Empty commands are not counted.
Quit
The quit command terminates the program.
Palindrome (Bonus Points)
Determines if the current string is a palindrome and prints an appropriate message.
Required Header File:
You are required to use the following function prototypes (but not limited to these) in your program.
#define HISTORY_SIZE 10 /*the maximum size of the history*/
#define MAX_STRING_LEN 100 /*the maximum size of strings*/
#define MAX_CMD_LEN 10 /*the maximum size of a command*/
/* Print a listing of the different commands */
void printHelp(void);
/* Read a string from the keyboard */
void getString(char *str);
/* Print the string to the screen */
void printString (const char *str);
/* Print a history of the commands */
void printHistory(char history[][MAX_CMD_LEN], int size);
/* Replace the current string with the new string */
void replaceString (char *current, char *newstr);
/* Toggle the case of the string */
void toggleString(const char *str, char *toggled);
/* Append the new string to the current string */
void appendString(char *str, char *newstr);
/* Reverse the string character by character*/
void reverseString (const char *str, char *reverse);
/* Reverse the string word by word */
void reverseWord (const char *str, char *wordreverse);
/* Find string str in the current string and return and number of times it appears in the current string */
int findString (const char *current, char *str);
/* Print the statistics of the current string */
void printStatistics (const char *str);
/* Add a command to the history table. If the history table is full, discard the oldest command. Function takes three arguments: history table, the command to be added into the table, and the current history table size */
void updateHistory (char history[][MAX_CMD_LEN], const char *cmd, int size);
/*Process the command entered from the keyboard. If the command is quit, returns 1. The result string contains the result of executing the command */
int processCommand (char *current, char *cmd, char *result);
Skeleton Code for Main:
int main(void)
{
char command[MAX_CMD_LEN];
char current[MAX_STR_LEN] = "";
char result[MAX_STR_LEN] = "";
char history_table[HISTORY_SIZE][MAX_CMD_LEN];
int history_size = 0;
int flag;
do {
printf("cmd> ");
scanf(%s,command);
if(strcmp(command, "hist")==0) {
printHistory(history_table, history_size);
}
else {
flag = processCommand(current, command, result);
}
/*If command is not empty, put command into history table */
} while (flag!=1);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started