I am basically trying to get user input as a string, and cut it into several different ones where each one of them is stored in a position in the array, I want to cut the strings when the user inputs a comma (,). For example, let's assume we declare an array of chars word[4]
with 4 spaces in it, and the user inputs "strawberry,banana,apple,orange" I want my code to store it in the array such that word[0] = strawberry
word[1] = banana
word[2] = apple
word[3] = orange
. I am new to c and i read a post about fgets and it sounded very useful. I attached the code i had if you are interested, I had segmentaion fault (core dumped) error though. I am mainly looking for ideas to try and implement what i mentioned earlier, not necessarily a fix for the code. Thanks!
PS Most of the code i attached is NOT MINE it belongs to RoadRunner. I got most of the fgets code from them as that is where I came to know it from and it included error checking. link for their answer: https://stackoverflow.com/a/41518512/14964842
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUMSTR 1
#define BUFFSIZE 100
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
int main(void) {
char *words[NUMSTR];
char buffer[BUFFSIZE];
size_t i, j, lastStr = 0, count = 0, slen; /* can replace size_t with int if you prefer */
/* loops only for three input strings */
for (i = 0; i < NUMSTR; i++) {
/* read input of one string, with error checking */
printf("Enter a word: ");
if (fgets(buffer, BUFFSIZE, stdin) == NULL) {
fprintf(stderr, "Error reading string into buffer.\n");
exit(EXIT_FAILURE);
}
/* removing newline from buffer, along with checking for overflow from buffer */
slen = strlen(buffer);
if (slen > 0) {
if (buffer[slen - 1] == '\n') {
buffer[slen - 1] = '\0';
} else {
printf("Exceeded buffer length of %d.\n", BUFFSIZE);
exit(EXIT_FAILURE);
}
}
/* checking if nothing was entered */
if (!*buffer) {
printf("No string entered.\n");
exit(EXIT_FAILURE);
}
/* allocate space for `words[i]` and null terminator */
words[count] = malloc(strlen(buffer) + 1);
/* checking return of malloc, very good to do this */
if (!words[count]) {
printf("Cannot allocate memory for string.\n");
exit(EXIT_FAILURE);
}
/**
* Starting from here is my code.
* I am trying to check every character in the string till i find the comma.
* If it is found, then the program should store everything that is before that comma and the one before if it
* exists.
*/
for (i = 0; i < NELEMS(buffer)-1; i++)
{
if(buffer[i] = ",")
{
char tempStorage[i];
tempStorage[i] = malloc(strlen(buffer) + 1);
for(j = i-1; j >= lastStr; j--)
{
buffer[j] = tempStorage[j];
tempStorage[i] = '\0';
/* if everything is fine, copy over into your array of pointers */
strcat(words[count], tempStorage);
}
free(tempStorage[i]);
tempStorage[i] = NULL;
}
}
}
/* reading input is finished, now time to print and free the strings */
printf("\nYour strings:\n");
for (i = 0; i < count; i++) {
printf("words[%zu] = %s\n", i, words[i]);
free(words[i]);
words[i] = NULL;
}
return 0;
}