Question
A Chegg expert already answered my question, which is was USING C LANGUAGE! Write a command line utility that takes any number of words on
A Chegg expert already answered my question, which is was
USING C LANGUAGE!
Write a command line utility that takes any number of words on the command line and prints the words out, sorted according to the character at a certain specified position, which should also be specified via the command line.
If two strings have the same character at the position of interest, their order does not matter.
To solve the problem, you are ONLY allowed to use pointers throughout the program.
You are NOT allowed to use any arrays. In other words, you will not have a single bracket in your program.
/** * A command line utility that takes any number of words on the command * line and prints the words out. The first argument is a character index * by which to sort the words. */ #include#include void swap_words(char** words, int index_word_1, int index_word_2); int is_bigger(char* pointer_word_1, char* pointer_word_2, int position); char** sort(int number_words, char** words, int position); int main(int argc, char **argv) { if(argc < 3) { printf("Insufficient arguments "); return -1; } int position = atoi(*(argv+1)); sort(argc-2, argv+2, position); for(int i=2; i
and he answered me with this code:
#include
#include
void swap_words(char** word1, char** word2) {
char* temp = *word1;
*word1 = *word2;
*word2 = temp;
}
int is_bigger(char* word1, char* word2, int position) {
while (*word1 && *word2 && position--) {
if (*word1 != *word2) {
return *word1 > *word2;
}
word1++;
word2++;
}
return 0;
}
char** sort(int num_words, char** words, int position) {
int i, j;
for (i = 0; i < num_words - 1; i++) {
for (j = 0; j < num_words - i - 1; j++) {
if (is_bigger(*(words + j), *(words + j + 1), position)) {
swap_words(words + j, words + j + 1);
}
}
}
return words;
}
int main(int argc, char** argv) {
if (argc < 3) {
printf("Insufficient arguments ");
return -1;
}
int position = atoi(argv[1]);
char** sorted_words = sort(argc - 2, argv + 2, position);
for (int i = 0; i < argc - 2; i++) {
printf("%s ", *(sorted_words + i));
}
return 0;
}
so please use the code the chegg expert gave us and show images of where you put the values: "a b c" in the code which will then result in getting us "c b a"
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