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;
Related
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
I have two strings:
char *str1 = "this is a test";
char *str2 = "ts bd a";
I'm trying to write a function that returns a new string with the same chars from the two string without duplicates (also ' ' is duplicate). eg.:
char *retStr = GetSameChars(str1, str2); //returns "ts a";
How can I do that?
What I'm tried:
char *GetSameChars(char str1[], char str2[]) {
int found = -1, i , j = 0, biggest, index = 0;
char *retArr, *star = '*';
int str1Len, str2Len, count = 0;
str1Len = strlen(str1);
str2Len = strlen(str2);
biggest = str1Len > str2Len ? str1Len : str2Len;
retArr = (char *)malloc(sizeof(char) * count);
for (i = 0; i < str1Len; i++) {
for (j = 0; j < str2Len; j++) {
if (str1[i] == str2[j] && found == -1) {
count++;
found = j;
} else
if (str2[j] == str2[found])
str2[j] = star; //Throw an exception
}
found = -1;
}
retArr = (char *)malloc(sizeof(char) * count);
j = 0;
for (i = 0; i < str2Len; i++)
if (str2[i] != '*')
retArr[j++] = str2[i];
for (i = 0; i < str2Len; i++)
printf("%c", retArr[i]);
}
When I tried the line str2[j] = star; I got an exception.
What is my mistake?
My recommendations would be: keep it simple; get to know the C standard library; write less, test more.
Some specific problems with your code: you pass the wrong variable to malloc(); you estimate the answer to fit in the size of the larger of the two strings but it will actually fit into the smaller of the two; you modify an argument string str2[j] = star -- you should be treating the arguments as readonly; you malloc() retArr twice unnecessarily, leaking the first one when you allocate the second; your algorithm simply doesn't work.
Although a lookup table, as others have suggested, would be more efficient, let's use the standard library routine strchr() to solve this problem:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *getSameChars(const char *string1, const char *string2) {
size_t string1_length = strlen(string1);
size_t string2_length = strlen(string2);
size_t shortest = string1_length < string2_length ? string1_length : string2_length;
char *common_pointer, *common = malloc(shortest + 1);
*(common_pointer = common) = '\0';
for (size_t i = 0; i < string1_length; i++) {
// character found in both input strings, but not yet in common string
if (strchr(string2, string1[i]) != NULL && strchr(common, string1[i]) == NULL) {
*common_pointer++ = string1[i];
*common_pointer = '\0';
}
}
return common;
}
int main() {
char *stringA = "this is a test";
char *stringB = "ts bd a";
char *result = getSameChars(stringA, stringB);
printf("%s\n", result);
free(result);
return(0);
}
Your code complains because you are trying to assign a pointer to a char, to get the value inside a pointer you need to use the * operator like so:
*star;
a good way to check if a letter have already appeared(if you want to use it on all of the ascii table then 128) is to use a lookup table. first you will need to declare an array the length of all letters in the alphabet like so:
char lut[26];
If it is a global variable then it will be set to 0, then all you need to do is go to the index of the char you got and mark it as 1, a simple if will later be able to determine if a letter has already appeard.
example:
lut[toupper(somechar) - 'A'] = 1;
In this example you set the char in the lookup table that is equivalent to the somechar variable as 1, marking it has already appeared.
hope this helps.
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';
I had to define a Pascal style string as a structure and also define a C string, then to write a function to convert the C string "Mississippi" into a Pascal style string. Pascal style strings have member variables int with number of characters and char variable with string without '\0' at the end.
My problem is in for-loop function. How can I put chars from C string into a new defined Pascal style string ?
I am not allowed to use the string.h header file.
#include <stdio.h>
#include <stdlib.h>
typedef struct PascalString {
int size;
char *string;
} PString;
struct PascalString *newPascalString(char *stringC) {
int i;
PString *pascal_string = malloc(sizeof(struct PascalString));
pascal_string->size = 11;
pascal_string->string = malloc(sizeof(char) * pascal_string->size);
for(i = 0; i < pascal_string->size; i++) {
pascal_string[i] = stringC[i]; // here is my problem
}
return pascal_string;
}
int main()
{
char *c_string = "Mississippi";
newPascalString(c_string);
return 0;
}
Just change this
for(i = 0; i < pascal_string->size; i++)
with
for(i = 0; (i < 11) && (stringC[i] != 0) ; i++)
since C strings are '\0' terminated, you have to loop untill you find the terminating '\0' or you are out of space.
You can also compute the length of the string like this
for (i = 0 ; stringC[i] != '\0' ; ++i)
pascal_string->size++;
and then keep the loop as it was
for(i = 0; i < pascal_string->size; i++)
there is another issue with your code, this is worng
pascal_string[i] = stringC[i];
you should store the characters in the string member like this
pascal_string->string[i] = stringC[i];
Apparently you need this
#include <stdio.h>
#include <stdlib.h>
typedef struct PascalString {
int size;
char *string;
} PString;
PString *newPascalString(char *stringC) {
int i;
PString *pascal_string = malloc(sizeof(struct PascalString));
if (pascal_string == NULL) /* always check the return value of malloc */
return NULL;
pascal_string->size = 0;
for (i = 0; stringC[i] != '\0'; ++i)
++pascal_string->size;
pascal_string->string = malloc(pascal_string->size);
if (pascal_string->string == NULL) {
free(pascal_string);
return NULL;
}
for(i = 0; i < pascal_string->size; i++)
pascal_string->string[i] = stringC[i]; // here is my problem
return pascal_string;
}
int main()
{
char *c_string = "Mississippi";
PString *pascal_string;
pascal_string = newPascalString(c_string);
if (pascal_string != NULL) {
if (pascal_string->size == 11)
printf("the pascal string has length 11 excluding the terminating `'\\0'`.\n");
else
printf("the pascal string length is not 11.\n");
if (pascal_string->string != NULL) {
/* write to standard output exacly pascal_string->size characters */
/* printf will not work here, it needs `'\0'` at the end */
fwrite(pascal_string->string, 1, pascal_string->size, stdout);
printf("\n");
/* free pascal_string->string, allocated with
*
* pascal_string->string = malloc(pascal_string->size)
*/
free(pascal_string->string);
}
/* free pascal_string, allocated with
*
* pascal_string = malloc(sizeof(struct PascalString))
*/
free(pascal_string);
}
/* since pascal_string == NULL no need to call free */
return 0;
}
To create the pascal-style string, You need to make some changes to your code.
As you mentioned, the problem is
pascal_string[i] = stringC[i];
pascal_string is a variable of type PString *. What you're trying to access is actually pascal_string->string[i].
Note: instead of using a hard-coded value of 11, you can determine the length of supplied string and allocate memory accordingly. [Equivalent of strlen()]
pascal_string->string = malloc((my_strlen(stringC)+1)); //allocate one more byte than supplied string size
if (pascal_string->string){ //check malloc success.
for(i = 0; (stringC[i] != 0); i++) { //continue upto end of input string
pascal_string->string[i] = string[i];
}
pascal_string->string[i] = 0; //terminating null
}
return pascal_string; //return
Pseudo-code for my_strlen():
int my_strlen(char * ip)
{
int i = 0;
for (i = 0; ip[i] != '\0'; i++);
return i;
}
I'm trying to take a string and break it into "word" components and store that in an array of strings.
"Hello my name is Bill." should give back a char** with elements, "Hello", "my", "name", "is", and "Bill."
My code will compile however I keep encountering a runtime error (I don't get warnings anymore and my debugger gdb doesn't work)>
I'm running on minGW on Window 8.
#include <stdio.h>
#include <stdlib.h>
char** words(char* string)
{
int i = 0;
int j = 0;
int k =0;
int count = 0;
char** stringArray = (char**) malloc(sizeof(char)*30*30);
while( string[i] != '\0' )
{
if(string[i] != ' ')
{
j =0;
while(string[i+j+1] != ' ')
{
j++;
}
i = i+j;
for(k=0; k<=j; k++)
{
stringArray[count][k] = string[i+k];
}
count++;
}
i++;
}
return stringArray;
}
int main()
{
char message[20] = "abcd efgh ijkl mno";
char** wordArray = words(message);
printf("%c\n\n", wordArray[0][0]);
int i =0;
while(wordArray[i])
{
printf("%s\n", wordArray[i]);
i++;
}
printf("\nThe problem is not with the words function");
return 0;
}
There are couple of issues that have been mentioned in the comments.
The allocation should look something like:
#include <ctype.h> // for isspace()
#define MAXSTRLEN 30 // using a symbolic constant
char **stringArray;
int i, j, k;
stringArray = malloc(sizeof(char*) * MAXSTRLEN); // don't cast from malloc
for (i = 0; i < 30; ++i) {
stringArray[i] = malloc(sizeof(char) * MAXSTRLEN);
}
// TODO error checking: malloc could return NULL
while copying the substrings would look like:
i = 0;
j = 0;
while( string[i] != '\0') // go through the whole string
{
while (string[i] != '\0' && isspace(string[i])) {
i++; // skip whitespaces
}
k = 0;
while (string[i] != '\0' && !isspace(string[i])) { // copy word until whitepace or end of string
stringArray[j][k++] = string[i++];
}
stringArray[j][k] = '\0'; // EOS !!!
j++;
}
and printing (j is number of words actually read):
for (i = 0; i < j/*30*/; ++i) { // (!) how to print
printf("%s\n", stringArray[i]);
}
And, yes strtok would also do the job.
In words() you're assigning values to stringArray as a two-dimensional array, and in main() you're reading values from it as an array of pointers. Those are not the same thing.
So you need to change it so that you're consistently treating it as a 2D array, or so that you're consistently treating it as an array of pointers (char* to be exact). Either will work... see the comments above for elaboration.
This code is all wrong.
char** stringArray = (char**) malloc(sizeof(char)*30*30);
First of all, sizeof(char) is always one, second, you don't need to cast a void. So:
char **stringArray = malloc(30 * 30);
But that doesn't make any sense because it's an array of char *, so you should allocate in terms of that:
char **stringArray = malloc(sizeof(char *) * 30);
Or even better:
char **stringArray = malloc(sizeof(*stringArray) * 30);
So now you have an array with 30 char *, but each of those is not initialized, so you need to do that:
for (i = 0; i < 30; i++)
stringArray[i] = malloc(sizeof(**stringArray) * 30);
If you don't do that, you can't access stringArray[count][k].
And then you assume the last element in the array is NULL, but you never set it, so you either do stringArray[count] = NULL at the end of words(), or you do calloc() instead of malloc().
I'm not analyzing the code beyond that; it's just all wrong.