1

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];
                }
            }
        }
5
  • 3
    When the strings are equal, the return value of strcmp is zero. Commented Sep 19, 2011 at 4:37
  • yes i know that, im checking to see if there is no match, if there isnt add the string to the array Commented Sep 19, 2011 at 4:38
  • Is this homework? If so, that's fine, but it should have the homework tag Commented Sep 19, 2011 at 4:47
  • Your question kinda doesn't make sense the way you're asking it. You can't "append" to the list; it's a fixed size. If you mean the array isn't full at the start, you have to keep track of how many words are in it. Is that what you're trying to do? Commented Sep 19, 2011 at 5:11
  • maybe i used the wrong wording. uni[100][16] is initially empty, string 1 is not, i want it scan the array for a match and if there is no match in uni add the string to the array. Commented Sep 19, 2011 at 5:14

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for typing that all out, ill see what i can take from it,some of its a bit advanced for me
I'm adding the char[][] ... one sec.
oh i see bit easier for me to understand
FYI = strncpy() is a safe version of strcpy() - the third argument limits the number of characters it will copy so that you can't overflow a buffer.
thanks Brian ,your second version was much easier for me to understand, i get it now
|
2

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];
    }
}

6 Comments

string1 is declared as this char string1[16];
@Bob: you need to make sure that the string is null-terminated '\0' before you copy it.
invalid conversion from char' to const char*'
@Bob: In case of char string1[16], string1 is your pointer to string, string1[16] is an array of chars.
@Bob: Exactly, and you don't need the nested for loop.
|
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.