In: Computer Science
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++;
}
}
}
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