Home > Software engineering >  I made a program, but can't seem to find the error, anyone has got a clue?
I made a program, but can't seem to find the error, anyone has got a clue?

Time:05-11

#include <stdio.h>

int main() { //main function
    char string1[400] = "Pinot noir is a red wine grape variety of the species Vitis vinifera. The name may also refer to wines created predominantly from Pinot noir grapes. The name is derived from the French words for pine and black. The word pine alludes to the grape variety having tightly clustered, pine cone-shaped bunches of fruit.[1].";
    char string2[400] = "Metallica is an American heavy metal band. The band was formed in 1981 in Los Angeles by vocalist/guitarist James Hetfield and drummer Lars Ulrich, and has been based in San Francisco for most of its career.[1][2] The band's fast tempos, instrumentals and aggressive musicianship made them one of the founding big four bands of thrash metal, alongside Megadeth, Anthrax and Slayer.";

    char str[15]; //string declaration
    printf("Enter the word to be searched : ");
    scanf_s("%s", str); //taking word from user

    int i, k = 0, length = 0, count1 = 0, count2 = 0; //declaring required variables
    for (i = 0; str[i] != '\0'; i  ) { //calculate length of given word
        length  ;
    }

    for (i = 0; string1[i] != '\0'; i  ) { //loop through string1
        if (str[k] == string1[i]) { //if character of word equals to character of string1
            i  ;
            if (k == length && string1[i - length] == ' ' && string1[i   1] == ' ') { //checking spaces front and back of the word in string1
                count1  ; //incrementing count
                k = 0;
            }
        } else {
            k = 0;
        }
    }
    for (i = 0; string2[i] != '\0'; i  ) { //same logic as we found word in string1
        if (str[k] == string2[i]) {
            k  ;
            if (k == length && string1[i - length] == ' ' && string1[i   1] == ' ')      {
                count2  ;
                k = 0;
            }
        } else {
            k = 0;
        }
    }
    printf("'%s' is found %d times in the String1.\n", str, count1); 
    //printing count in String1
    printf("'%s' is found %d times in the String2.", str, count2); 
    //printing count in String2
}

CodePudding user response:

The compiler complains because you are using scanf_s incorrectly.

Microsoft insists on using scanf_s instead of scanf, but just changing the function name is insufficient: you should pass the destination array size in addition to the destination array pointer.

The problem is Microsoft's version of scanf_s API differs from the C Standard version and this function is not supported on many platforms, making it non portable.

You should use the standard function scanf and pass the maximum number of characters to store into str this way:

   if (scanf("s", str) != 1) {
       fprintf(stderr, "invalid or missing input\n");
       return 1;
   }

To prevent the compiler error, you can add this definition before the #include <stdio.h>:

#define _CRT_SECURE_NO_WARNINGS 1

You scanning code is incorrect too:

  • you will miss some matches such as aab in aaab

  • the test for separators is incorrect and will fail or cause undefined behavior in all cases.

  • you should use a function to avoid duplicating the code.

Here is a modified version:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

size_t count_matches(const char *s, const char *str) {
    size_t i, k, count = 0;

    for (i = 0; s[i] != '\0'; i  ) {
        if (i == 0 || s[i - 1] != ' ') {
            // if at the beginning of a word, try matching
            for (k = 0;; k  ) {
                if (str[k] == '\0') {
                    // check for end of word in s
                    if (s[i   k] == '\0' || s[i   k] == ' ')
                        count  ;
                    break;
                }
                if (s[i   k] != str[k])
                    break;
            }
        }
    }
    return count;
}

int main() { //main function
    char string1[] = "Pinot noir is a red wine grape variety of the species Vitis vinifera. The name may also refer to wines created predominantly from Pinot noir grapes. The name is derived from the French words for pine and black. The word pine alludes to the grape variety having tightly clustered, pine cone-shaped bunches of fruit.[1].";
    char string2[] = "Metallica is an American heavy metal band. The band was formed in 1981 in Los Angeles by vocalist/guitarist James Hetfield and drummer Lars Ulrich, and has been based in San Francisco for most of its career.[1][2] The band's fast tempos, instrumentals and aggressive musicianship made them one of the founding big four bands of thrash metal, alongside Megadeth, Anthrax and Slayer.";

    char str[15];

    printf("Enter the word to be searched : ");
    if (scanf("s", str) != 1) {
        fprintf(stderr, "invalid or missing input\n");
        return 1;
    }

    int count1 = count_matches(string1, str);
    int count2 = count_matches(string2, str);

    printf("'%s' is found %d times in the String1.\n", str, count1); 
    printf("'%s' is found %d times in the String2.\n", str, count2); 
    return 0;
}

CodePudding user response:

Maybe you should consider using 'gets(str)' function instead of 'scanf_s' (or even 'scanf').

  •  Tags:  
  • c
  • Related