Question

In: Computer Science

This program is used to split the input based on the delimiter. Change the code below...

This program is used to split the input based on the delimiter. Change the code below so that the parse function should return an array that contains each piece of the word. For example, the input to the function would be something like 'aa:bbbA:c,' which would be a char *. The function's output would then be a char ** where each element in the array is a char *. For this example, it would return some result where result[0] = 'aa', result[1] = 'bbbA', and result [2] = 'c'. So instead of doing the printing in the parse func, return each piece of the word, pass it to the main function, then do the printing in the main

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void parsefunc (char, char*);
int string_length (char*);
char* my_strncpy (char*, const char*, unsigned int);

//Main Fucntion
int main(int argc, char *argv[] ) {

   if(argc == 1 || argc == 2){
       printf("Not enough arguments, please enter three agruments with program name, delimiter and text! \n");
   }
   else{  
   parsefunc(*argv[1], argv[2]);
   }

}

//This function is used to splip up the string and print the results
void parsefunc(char delimeter, char *input){
int count = 0;
int current = 0;
int length = strlen(input);

   //Loop through the string
for (int i = 0; i < length; i++){
       //Check for delimiter
if(input[i] == delimeter){
  
char* tempString = malloc(sizeof(char) * (count + 1));
strncpy(tempString, input+current, count);

           tempString[count+1] = '\0';
//printf("%d\n", current);
current += count+1;
printf("%s Length %d\n", tempString, count);
count = 0;
           free(tempString);

}
       //Check for delimiter
       else if(input[i+1] == '\0'){
char* tempString = malloc(sizeof(char) * (count + 1));
strncpy(tempString, input+current, count+1);

           tempString[count+1] = '\0';
//printf("%d\n", current);
current += count+1;
printf("%s Length %d\n", tempString, count+1);
           free(tempString);
}else{
count++;
}

}
}

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

--------------main.c---------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char ** parsefunc(char, char *, int *);
int string_length(char*);
char *my_strncpy(char *, const char *, unsigned int);

//Main Fucntion
int main(int argc, char *argv[])
{

   if (argc == 1 || argc == 2)
   {
       printf("Not enough arguments, please enter three agruments with program name, delimiter and text! \n");
   }
   else
   {
       int result_size;   //this variable will store the size of the result array
       char **result = parsefunc(*argv[1], argv[2], &result_size);   //splits and returns char **
       int i = 0;
       for(i = 0; i < result_size; i++){   //print the strings of results array
           printf("String: \"%s\" Length: %d\n", result[i], (int)strlen(result[i]));
       }
   }
}

//This function is used to split up the string and print the results
//The function's output would then be a char ** where each element in the array is a char *.

char ** parsefunc(char delimeter, char *input, int *result_size)
{
   int count = 0;
   int current = 0;
   int length = strlen(input);

   int delimeter_count = 0;   //count the number of time delimeter occurred, so that you know how many pointers char* are required
   for (int i = 0; i < length; i++)
       if (input[i] == delimeter)
           delimeter_count++;

   char **result = malloc(sizeof(char *) * (delimeter_count + 1));   //allocate space for total (delimeter_count + 1) of char * pointers

   int result_index = 0;   //this variable will track the index where the new element is to be inserted in result array
   //Loop through the string
   for (int i = 0; i < length; i++)
   {
       //Check for delimiter
       if (input[i] == delimeter)
       {

           char *tempString = malloc(sizeof(char) *(count + 1));
           strncpy(tempString, input + current, count);

           tempString[count + 1] = '\0';
           //printf("%d\n", current);
           current += count + 1;
           //printf("%s Length %d\n", tempString, count);
           count = 0;
           //free(tempString);           //do not free because we will be using same memory and store its pointer in result array
           result[result_index++] = tempString;    //store the pointer to the tempString in result array
       }
       //Check for delimiter
       else if (input[i + 1] == '\0')
       {
           char *tempString = malloc(sizeof(char) *(count + 1));
           strncpy(tempString, input + current, count + 1);

           tempString[count + 1] = '\0';       
           //printf("%d\n", current);
           current += count + 1;
           //printf("%s Length %d\n", tempString, count + 1);
           //free(tempString);
           result[result_index++] = tempString;    //store the pointer to the tempString in result array
       }
       else
       {
           count++;
       }
   }
   *result_size = result_index;   //write the size of result arn tray
   return result;                    //return the char **
}

----------Screenshots-----------------

----------Output--------------

-----------------Updated code--------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char ** parsefunc(char, char *);
int string_length(char*);
char *my_strncpy(char *, const char *, unsigned int);

//Main Fucntion
int main(int argc, char *argv[])
{
   if (argc == 1 || argc == 2)
   {
       printf("Not enough arguments, please enter three agruments with program name, delimiter and text! \n");
   }
   else
   {
       char **result = parsefunc(*argv[1], argv[2]);   //splits and returns char **
       int i = 0;
       while(result[i]){           //print the strings of results array
           printf("String: \"%s\" Length: %d\n", result[i], (int)strlen(result[i]));
           i++;
       }
   }
}

//This function is used to split up the string and print the results
//The function's output would then be a char ** where each element in the array is a char *.
char ** parsefunc(char delimeter, char *input)
{
   int count = 0;
   int current = 0;
   int length = strlen(input);
   int i = 0;
   int delimeter_count = 0;   //count the number of time delimeter occurred, so that you know how many pointers char* are required
   for (i = 0; i < length; i++)
       if (input[i] == delimeter)
           delimeter_count++;
   printf("%d\n", delimeter_count);

   char **result = malloc(sizeof(char *) * (delimeter_count + 2));   //allocate space for total (delimeter_count + 2) of char * pointers
   for(i = 0; i < delimeter_count+2; i++)
       result[i] = NULL;

   int result_index = 0;   //this variable will track the index where the new element is to be inserted in result array
   //Loop through the string
   for (i = 0; i < length; i++)
   {
       //Check for delimiter
       if (input[i] == delimeter)
       {

           char *tempString = malloc(sizeof(char) *(count + 1));
           strncpy(tempString, input + current, count);

           tempString[count + 1] = '\0';
           //printf("%d\n", current);
           current += count + 1;
           //printf("%s Length %d\n", tempString, count);
           count = 0;
           //free(tempString);           //do not free because we will be using same memory and store its pointer in result array
           result[result_index++] = tempString;    //store the pointer to the tempString in result array
       }
       //Check for delimiter
       else if (input[i + 1] == '\0')
       {
           char *tempString = malloc(sizeof(char) *(count + 1));
           strncpy(tempString, input + current, count + 1);

           tempString[count + 1] = '\0';       
           //printf("%d\n", current);
           current += count + 1;
           //printf("%s Length %d\n", tempString, count + 1);
           //free(tempString);
           result[result_index++] = tempString;    //store the pointer to the tempString in result array
       }
       else
       {
           count++;
       }
   }
   return result;                    //return the char **
}

------------------------------------------------

Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

How does the marginal product of an input change as less of that input is used?
How does the marginal product of an input change as less of that input is used?
Write a program with total change amount as an integer input, and output the change using...
Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes c++ please
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Write a program in pyton with total change amount as an integer input, and output the...
Write a program in pyton with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. Ex: If the input is: 0 (or less than 0), the output is: No change Ex: If the input is: 45 the output is: 1 Quarter 2 Dimes Can you...
Change Calculator Task: Create a program that will input a price and a money amount. The...
Change Calculator Task: Create a program that will input a price and a money amount. The program will then decide if there is any change due and calculate how much change and which coins to hand out. Coins will be preferred in the least number of coins (starting with quarters and working your way down to pennies). Input: total_price, amount_tender allow the user to input 'q' for either value if they want to quit the program Validate: total_price cannot be...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
a) Which lines of code contain operations that change the contents of memory in this program?...
a) Which lines of code contain operations that change the contents of memory in this program? b) What are those operations? int main ( void ){             double dScore1;             double dScore2;             double dScore3;             double dAverage;             printf("Enter score 1: ");                                             //line 1             scanf("%lg", &dScore1);                                           //line 2             printf("Enter score 2: ");                                             //line 3             scanf("%lg", &dScore2);                                           //line 4             printf("Enter score 3: ");                                             //line 5             scanf("%lg", &dScore3);                                           //line 6             dAverage = (dScore1 + dScore2...
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT