In the main function, the word is entered in char[n] . In another function, the word is read from the text file into the rootie array, then the word is compared with the string from the rootie . If several characters of a word match an element from rootie , the int root counter is incremented by 1. Then the word is compared with the next line, and so on.

  1. When I write a comparison, both strcmp and strncmp give the error "argument of type" char "is incompatible with the parameter of type" const char "":

  2. Will this code work as it should?

     for (int i = 0; i > counter2; i++) { while (counter2) { if (strcmp(word, rootie[i])) root++; counter2++; } } 
  • one
    how do you declare word и rootie[i] ? - pavel
  • @pavel char word[40]; scanf("%s", &word[i]); char word[40]; scanf("%s", &word[i]); and char **rootie; *rootie = new char[counter2]; for (int i = 0; i > counter2; i++) { for (int j = 0; j > 20; j++) { fscanf(roots, "%s", rootie); } } char **rootie; *rootie = new char[counter2]; for (int i = 0; i > counter2; i++) { for (int j = 0; j > 20; j++) { fscanf(roots, "%s", rootie); } } char **rootie; *rootie = new char[counter2]; for (int i = 0; i > counter2; i++) { for (int j = 0; j > 20; j++) { fscanf(roots, "%s", rootie); } } . An error for the word. - Katya Gibteva
  • scanf("%s", &word[i]); uh ... you EXACTLY know what you are doing? fscanf(roots, "%s", rootie); I can not imagine how it is compiled. - pavel
  • *rootie = new char[counter2]; - I'm not sure what exactly you wanted. Maybe rootie = new char* [counter2]; ? In general, I advise you to put all the code here and look carefully at data types (char char * and char **). - pavel

1 answer 1

A comparison of two strings in C is done like this:

 #include <stdio.h> #include <string.h> int main() { char a[100], b[100]; printf("Enter the first string\n"); fgets(a, sizeof(a), stdin); printf("Enter the second string\n"); fgets(b, sizeof(b), stdin); if (strcmp(a,b) == 0) printf("Entered strings are equal.\n"); else printf("Entered strings are not equal.\n"); }