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...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
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...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
In C++ and input code using the template below Get a copy of the caseswitch.cpp. The...
In C++ and input code using the template below Get a copy of the caseswitch.cpp. The purpose of the program is to demonstrate how to use Switch statement. Complete the for loop so that the program will ask the user for 6 input grades. Compile and run this C++ program. Use input value A, B, C, D, E, and F. Notice there is no output for B and E. Add cases for them. B is "Good Work." Add a case...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT