making an array-based program work with no elements - c

The exercise it to take an array of strings and, using pointers, concatenate them in a new string. I made it work by hard coding the values at each string array index, but I can't figure out how to make it work if the array has NO elements. I think I need to entirely restructure my code...
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
char *newArray[3];
//original code didn't have newArray as a pointer, and had the following:
//char *firstString = &newArray[0]; This was pointing to the value at the address of newArray[0] which was null at this point in time
//firstString = "Knicks"; This was reassigning the value of firstString to "Knicks" instead of pointing to the value located at the proper index address.
//this code correctly assigns values to each index of the array (which itself is just a pointer that is pointing to these values
newArray[0] = "Knicks";
newArray[1] = "Warriors";
newArray[2] = "Bulls";
//this code correctly assigns pointers that point to the index values of the array, which are all stated above.
char *firstString = newArray[0];
char *secondString = newArray[1];
char *thirdString = newArray[2];
int firstStringCount = 0;
int secondStringCount = 0;
int thirdStringCount = 0;
//count length of first element
for (int i = 0; i < 1000; i++) {
if (firstString[i] == '\0') {
break;
}
else {
firstStringCount++;
}
}
//count length of second element
for (int i = 0; i < 1000; i++) {
if (secondString[i] == '\0') {
break;
}
else {
secondStringCount++;
}
}
//count length of third element
for (int i = 0; i < 1000; i++) {
if (thirdString[i] == '\0') {
break;
}
else {
thirdStringCount++;
}
}
//int len = firstStringCount + secondStringCount + thirdStringCount;
char *concatenatedString = malloc(firstStringCount + secondStringCount + thirdStringCount);
for (int i = 0; i < firstStringCount; i++) {
concatenatedString[i] = firstString[i];
}
for (int i = 0; i < secondStringCount; i++) {
concatenatedString[i+firstStringCount] = secondString[i];
}
for (int i = 0; i < thirdStringCount; i++) {
concatenatedString[i+firstStringCount+secondStringCount] = thirdString[i];
}
//add in null value
concatenatedString[firstStringCount + secondStringCount + thirdStringCount] = '\0';
printf("%s\n", concatenatedString);
printf("%i\n", firstStringCount);
printf("%i\n", secondStringCount);
printf("%i\n", thirdStringCount);
return 0;
}

Your program will work when you have empty strings. This is not the same as having no array elements, because there must still be a \0 terminator in each, such as would be created with
newArray[0] = "";
But you have not allocated enough memory for the destination's string terminator. By using a 1 +
char *concatenatedString = malloc(1 + firstStringCount + secondStringCount +
thirdStringCount);
you now have room for the terminator with
concatenatedString[firstStringCount + secondStringCount + thirdStringCount] = '\0';

Related

How to store a substring given a delimiter in C

Let's say I have a series of data that's in this form:
"SomethingIDontCareAbout : SomethingICareAbout"
where the part after the ":" can vary in length of course.
The goal here is only storing the "SomethingICareAbout" substring efficiently. I made this function but the problem is that I'm storing both substrings,so it seems like a waste of memory. Any help to reduce to the time/space complexity?
char** ExtractKey(char* S)
{
int n = strlen(S);
int count = 0, i = 0, j = 0;
for(i = 0; i < n; i++)
{
if(S[i] == ':')
break;
count++;
}
char** T = (char**)malloc(2 * sizeof(char*));
T[0] = (char*)malloc((count + 1) * sizeof(char));
T[1] = (char*)malloc((n - count) * sizeof(char));
for(i = 0; i < count; i++) // inefficient ? cus we won't need T[0] [j]
{
T[0][j] = S[i];
j++;
}
T[0][j+1] = '\0';
j = 0;
for(i = count + 1; i < n; i++)
{
T[1][j] = S[i];
j++;
}
T[1][j+1] = '\0';
return T;
}
There is no reason to invent a search for a character in a string, or a copy of a string.
If the input data will live long enough for you to use the "value" part, just return a pointer to the value:
char* ExtractKey(char* S)
{
return strchr(S, ':');
}
If it doesn't, or if you for some reason need a separate copy:
char* ExtractKey(char* S)
{
return strdup(strchr(S, ':'));
}
Honestly, this could be done efficiently if strtok() was used to split those strings. I have designed the following code that parses each string of a 2-D array with a common delimiter that is : here.
Now, let's take a look into the code (notice the comments):
#include <stdio.h>
#include <string.h>
#define MAX_LEN 128
int main(void) {
// The 2-D string
char str[][MAX_LEN] = {"SomethingElse : SomethingToCareAbout",
"Something2 : SomethingToCare2",
"Unnecessary : Necessary"};
int size = sizeof(str) / sizeof(str[0]);
// Applying Variable-Length Array (valid in C)
char store_cared_ones[size][MAX_LEN];
for (int i = 0; i < size; i++) {
// Declaring a temporary pointer variable to obtain the required
// substring from each string
char *sub_str = NULL;
sub_str = strtok(str[i], ": ");
sub_str = strtok(NULL, ": ");
// Copying the 'sub_str' into each array element of 'store_cared_ones'
strcpy(store_cared_ones[i], sub_str);
}
// Displaying each of 'store_cared_ones'
for (int i = 0; i < size; i++)
fprintf(stdout, "%s\n", store_cared_ones[i]);
return 0;
}
Finally, let's see what that code does:
rohanbari#genesis:~/stack$ ./a.out
SomethingToCareAbout
SomethingToCare2
Necessary

Selection of unique characters

Please, help with the code.
Requirement:
Write a function my_union that takes two strings and returns, without doubles, the characters that appear in either one of the strings.
Example:
Input: "zpadinton" && "paqefwtdjetyiytjneytjoeyjnejeyj"
Output: "zpadintoqefwjy"
My code:
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
char *my_union(char *a, char *b) {
char *str;
// Algorithm for excluding nonunique characters from string a(given in
// parameters).
str[0] = a[0];
int k = 1;
str[k] = '\0';
for (int i = 1; a[i] != '\0'; i++) {
bool is = true;
for (int j = 0; str[j] != '\0'; j++) {
if (str[j] == a[i]) {
is = false;
break;
}
}
if (is) {
str[k] = a[i];
k++;
str[k] = '\0';
}
} // In this case we are excluding excess character 'n' from "zpadinton", so
// str is equal to "zpadinto".
// Algorithm for adding unique characters from array b(given in parameters)
// into str.
for (int i = 0; b[i] != '\0'; i++) {
bool is = true;
for (int j = 0; str[j] != '\0'; j++) {
if (str[j] == b[i]) {
is = false;
break;
}
}
if (is) {
strncat(str, &b[i], 1);
}
}
return str;
}
The first algorithm is almost identical with second, but it doesn't work(. Mb I messed up with memory, give some advice, pls.
If you mean, get the unique characters from two strings and store them into a new string, try this code ;
First, you must allocate a memory for str. In your code, str is not pointing allocated memory location, so you will probably get segmentation fault.
int contains(const char * str,char c)
{
for (int i = 0; i < strlen(str); ++i)
if(str[i] == c)
return 1;
return 0;
}
char * my_union(char *a, char*b)
{
char * res = (char*)malloc(sizeof(char)*(strlen(a) + strlen(b)));
int pushed = 0;
for (int i = 0; i < strlen(a); ++i)
{
if(!contains(res,a[i])){
res[pushed] = a[i];
pushed++;
}
}
for (int i = 0; i < strlen(b); ++i)
{
if(!contains(res,b[i])){
res[pushed] = b[i];
pushed++;
}
}
return res;
}
int main(int argc, char const *argv[])
{
char string1[9] = "abcdefgh";
char string2[9] = "abegzygj";
char * result = my_union(string1,string2);
printf("%s\n", result);
return 0;
}
Also, do not forget the free the return value of my_union after you done with it.

Problems with Heap Corruption - C

I am making a "simple" print out string, append string and remove section from a string. The append and new string works sometimes, sometimes it outputs nothing.
When i do:
char * temp = malloc(newSize);
It just stops outputting anything.
I have commented everything out in sections, trying to find the problem. Can't seem to find the problem, but google keeps coming up with "Heap Corruption".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char * data;
int length;
} String;
int str_getLength(const char * characters)
{
int index = 0;
while (1)
{
if (characters[index] == '\0') break;
index++;
}
return index;
}
String str_new(const char * characters)
{
String result;
result.length = str_getLength(characters);
result.data = malloc(result.length);
memcpy(result.data, characters, result.length);
return result;
}
void str_append(String * str, const char * characters)
{
int charsLength = str_getLength(characters);
str->data = realloc(str->data, charsLength);
for (int i = 0; i < charsLength; i++) {
str->data[i + str->length] = characters[i];
}
str->length = str->length + charsLength;
}
void str_remove(String * str, int startIndex, int endIndex)
{
if (startIndex < 0 || endIndex > str->length || endIndex < startIndex) {
return;
}
int chunkSize = endIndex - startIndex;
int newSize = str->length - chunkSize;
char * temp = malloc(newSize);
// for (int i = 0; i < str->length; i++)
// {
// if (i < startIndex || i > endIndex) {
// temp[i] = str->data[i];
// }
// }
// free(str->data);
// str->length = newSize;
// str->data = temp;
}
}
int main()
{
String str = str_new("Hello, ");
printf("%s\n", str.data);
str_append(&str, "this is my first C application.");
printf("%s\n", str.data);
str_remove(&str, 0, 3);
printf("%s\n", str.data);
free(str.data);
return 0;
}
I expected it to output a modified string, it doesn't and sometimes it outputs nothing.
I am a beginner, sorry if it's a quick fix.
Apart from the blaze answer.
There are few more problems.
// for (int i = 0; i < str->length; i++)
// {
// if (i < startIndex || i > endIndex) {
// temp[i] = str->data[i];
// }
// }
You will access out of bound for temp.
You need to maintain separate index for temp.
char * temp = malloc(newSize+1);
int k=0;
for (int i = 0; i < str->length; i++)
{
if (i < startIndex || i > endIndex) {
temp[k++] = str->data[i];
}
}
temp[k] = '\0';
free(str->data);
str->length = newSize;
str->data = temp;
And
You are not null terminating the string after append.
str->data = realloc(str->data, str->length + charsLength +1); //current length + new length + \0
for (int i = 0; i < charsLength; i++) {
str->data[i + str->length] = characters[i];
}
str->data[i + str->length] = '\0'; //null terminate the new string
str->length = str->length + charsLength;
There are two problems with your reallocation. First of all, you're not assigning the result of the realloc to str->data, so if the memory was reallocated to a different place, tr->data points to invalid memory afterwards. Second, you're not adding the sizes of the string and the appended part, you're just taking the size of the part that you're appending.
This here
realloc(str->data, charsLength);
Should be:
str->data = realloc(str->data, charsLength + str->length + 1);

Using realloc on a char * within an array alters data outside of that array

I'm observing some really weird behavior regarding realloc .... I was wondering if y'all could help me.
I have an array of dynamically allocated char *'s called "frags". I also have a char * called "combination" which points to some string literal that represents a new fragment. I want to replace one of the fragments within "frags" with the contents of "combination." The way my project is structured, I am sending the frags array, index of to-be-replaced frag, and combination string into a function. Within this function I have:
printf("combination before realloc: %s\n", combination);
char *newString = (char *) realloc(frags[firstIndex], strlen(combination) + 1);
assert(newString != NULL);
printf("combination after realloc: %s\n", combination);
strcpy(newString, combination);
frags[firstIndex] = newString;
Oddly, the printf's do not print the same thing. The first printf yields "hellol" which is correct, but the next printf yields jibberish - something like "{?`?p??". Thus, the problem resides in the call to realloc. And I honestly have no idea what's going on. It seems the very call to realloc has messed with combination somehow, but I thought that if that could possibly happen then it would return NULL?
Please help me :(
Edit: Adding code
bool findMaxOverlap(char *first, char *second, char **combination, int *roundMax) {
// setup lng and shrt
char *lng, *shrt;
if (strlen(first) >= strlen(second)) { lng = first; shrt = second; }
else { lng = second; shrt = first; }
int shrtLen = strlen(shrt), lngLen = strlen(lng);
// check if lng contains shrt
if (strstr(lng, shrt) != NULL && shrtLen > *roundMax) {
*combination = lng;
*roundMax = shrtLen;
return true;
}
else // check if lng's tail ends contain part of shrt
{
int numChars = shrtLen - 1, max = 0, shrtOffset = 0, lngOffset = 0;
for (int i = 0; i < shrtLen && numChars > *roundMax && numChars > max; i++) {
numChars = shrtLen - 1 - i;
for (int j = 0; j < lngLen; j++) {
if (strncmp(shrt + i, lng + j, numChars) == 0) {
max = numChars;
shrtOffset = i;
lngOffset = j;
}
}
}
if (shrtOffset > lngOffset) {
// short first
char newFrag[lngLen + shrtOffset + 1];
strncpy(newFrag, shrt, shrtOffset);
strcat(newFrag, lng + shrtOffset);
*combination = newFrag;
*roundMax = numChars;
return true;
} else {
// lng first
char newFrag[lngLen + (shrtLen - numChars) + 1];
strcpy(newFrag, lng);
strcat(newFrag, shrt + numChars);
*combination = newFrag;
printf("combination in findmax is: %s\n", *combination);
*roundMax = numChars;
return true;
}
}
return false;
}
void mergeFrags(char *frags[], int index1, int index2, char *combination) {
int firstIndex, secondIndex;
if (index1 < index2) {
firstIndex = index1;
secondIndex = index2;
} else {
firstIndex = index2;
secondIndex = index1;
}
char temp[strlen(combination) + 1];
strcpy(temp, combination);
char *newString = (char *) realloc(frags[firstIndex], strlen(combination) + 1);
assert(newString != NULL);
strcpy(newString, temp);
frags[firstIndex] = newString;
free(frags[secondIndex]);
}
char *reassemble(char *frags[], int numFrags) {
if (numFrags > 1) {
char *combination;
int max, index1, index2, currNumFrags = numFrags;
for (int currentRound = 0; currentRound < numFrags - 1; currentRound++) {
max = index1 = index2 = 0, combination = NULL;
for (int i = 0; i < currNumFrags; i++) {
for (int j = i+1; j < currNumFrags; j++) {
//find max overlap of pair
if (findMaxOverlap(frags[i], frags[j], &combination, &max)) {
printf("round #: %d, combination: %s, max: %d\n", currentRound, combination, max);
index1 = i; index2 = j;
}
}
}
// merge
mergeFrags(frags, index1, index2, combination);
currNumFrags--;
}
}
return frags[0];
}
You said (in the comments above) that you were using strdup to allocate the data in combination, but what you're really doing is setting combination to point to data that's on the stack. After findMaxOverlap returns, you are now pointing at unallocated space on the stack, and this provides you with the undefined behavior you're seeing. When realloc is called, the area in the stack is reused, and the value of combination looks like garbage.

search array for string

How would i implement this?Im trying to compare a array of c strings to a single string and if there is no match append it to the 2d array.
char*mprt,uni[100][16];
mprt = &uni[0][0];
for (int s = 0;s <= 99;s++)
{
for (int z = 0;z <= 15;z++)
{
if (strcmp(mprt++, string1) != 0)
{
uni[s][z] = string1[z];
}
}
}
Ok ... from your comments I now get what you're trying to do. You'd want to make this into a function so you could feed words to it, but it should get you pointed in the right direction.
Note that you can use char[][], but this way your strings can be of any length because we dynamically allocate them when we put them in the list.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
/* space for 100 strings */
char **uni = calloc(100, sizeof(char*));
char **i;
/* Put one word in the list for test */
*uni = calloc(5, sizeof(char*));
strncpy(*uni, "this", 5);
/* here's the string we're going to search for */
char * str2 = "that";
/* go through the first dimension looking for the string
note we have to check that we don't exceed our list size */
for (i = uni; *i != NULL && i < uni+100; i++)
{
/* if we find it, break */
if (strcmp(*i,str2) == 0)
break;
}
/* if we didn't find the string, *i will be null
* or we will have hit the end of our first dimension */
if (i == uni + 100)
{
printf("No more space!\n");
}
else if (*i == NULL)
{
/* allocate space for our string */
*i = calloc(strlen(str2) + 1, sizeof(char));
/* copy our new string into the list */
strncpy(*i, str2, strlen(str2) + 1);
}
/* output to confirm it worked */
for (i = uni; *i != NULL && i < uni+100; i++)
printf("%s\n",*i);
}
For completeness, the char[][] version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char uni[100][16];
int i,j;
/* init our arrays */
for (i=0;i<100;i++)
for (j=0;j<16;j++)
uni[i][j] = '\0';
/* Put one word in the list for test */
strncpy(uni[0], "this",15);
/* here's the string we're going to search for */
char * str2 = "that";
/* go through the first dimension looking for the string */
for (i = 0; uni[i][0] != '\0' && i < 100; i++)
{
/* if we find it, break */
if (strcmp(uni[i],str2) == 0)
break;
}
/* if we didn't find the string, uni[i][0] will be '\0'
* or we will have hit the end of our first dimension */
if (i == 100)
{
printf("No more space!\n");
}
else if (uni[i][0] == '\0')
{
/* copy our new string into the array */
strncpy(uni[i], str2, 15);
}
/* output to confirm it worked */
for (i = 0; uni[i][0] != '\0' && i < 100; i++)
printf("%s\n",uni[i]);
}
Edit to explain C pointers and arrays from comments below:
In C, arrays degrade to pointers. This is actually really confusing when you first start.
If I have char myArray[10] and I want to pass that to a function that takes a char * argument, I can use either &myArray[0] or just myArray. When you leave off the index, it degrades to a pointer to the first element in the array.
In a multidimensional array like yours, &uni[5][0] == uni[5] - both are pointers to the first element in the second dimension at index 5 in the first. It degrades to char* pointed at the beginning of the 6th word in your list.
In your for loop, you need to copy the whole string to append it,
Replace the line by this,
strcpy(uni[s], string1[z]);
Considering string1[z] is an element of an array of char pointers.
Edit:
Not sure if this is what you're trying to do, but you'll end up with all elements set to string1
char string1[] = "String";
char uni[100][16] = {};
for (int s = 0; s < 100; s++)
{
if (strcmp(uni[s], string1) != 0)
{
strcpy(uni[s], string1);
}
}
Or this, without strcpy()
char string1[] = "String";
char uni[100][16] = {};
for (int s = 0; s < 100; s++)
{
for (int r = 0; r < sizeof(string1); r++)
{
uni[s][r] = string1[r];
}
}
to append to the end of the 2D array you need to use dynamic memory allocation
const int row_max = 100, col_max = 16;
char** uni = NULL;
char searchString[col_max] = "xyz";
int currentLength = 0;
uni = (char**) malloc (row_max * sizeof(char*)); //TODO:error handling code to be added
for (int row = 0; row < row_max; row++)
{
uni[row] = (char*)malloc(col_max * sizeof(char));//TODO:error handling code to be added
currentLength = row;
}
for (int row = 0; row < row_max; row++) //fill array uni with data here
{
uni[row] = "abc";
}
for (int row = 0; row < row_max; row++)
{
for (int col = 0; col < col_max; col++)
{
if (strcmp(&uni[row][col], searchString) != 0 )
{//string not found
uni = (char**)realloc(uni, (currentLength + 1) * sizeof(char*));//TODO:error handling code to be added
uni[currentLength + 1] = (char*)malloc(col_max);//TODO:error handling code to be added
currentLength++;
strcpy(uni[currentLength],searchString); //append at end of 2D array
goto stop;
}
}
}
stop:
for (int row = 0; row <= currentLength; row++)
free(uni[row]);
free(uni);
return 0;

Resources