Search code examples
cubuntucomparefield

Q: Compare Fields (Libfdr) and Array in C


Firstly, I'm a beginner. Sorry if it's a super easy question. I want to check some functions in the c file provided by the user and print them.

User input in Linux : $ ./question2 -s pro1.c

And Terminal Print : Counter: 0

This code cannot compare.

int main(int argc, char *argv[2])
{
   parameter = malloc(sizeof(char) * 2);
   if (argc >= 2)
   {
       parameter = argv[1];
       IS is; // #include "fields.h"
       char *functions[5] = {"strcat", "strcpy", "sprintf", "gets", "getpw"};
       is = new_inputstruct(argv[2]);
       int i, counter;

       while (get_line(is) >= 0)
       {
           //printf("%s\n", *is->fields);
           for (i = 0; is->fields[i] != NULL; i++)
           {
               if (strcmp(is->fields[i], "strcpy") == 0) // if true
               {
                   printf("%s\n", is->fields[i]);
                   counter++;
               }
           }
       }
       printf("Counter: %d\n", counter);

Solution

  • You can use compare 2 string with strstr function.

    if(strstr(is->fields[i],"strcpy")){
       printf("%s\n", is->fields[i]);
       counter++;
    }