0

I just started programming in C, and decided to write a function to search for a substring in a string to test myself(Yes, I am aware there is a function for that already). However, the program will sometimes give the 'correct' answer but it will also give a seemingly senseless number sometimes. Does it have anything to do with my use of printf? Thanks in advance :).

#include <stdio.h>

int check_char_exists(char fvar_string[],char fvar_chartosearchfor);
int check_string_exists(char fvar_string[],char fvar_stringtosearchfor[]);

int main() {
    char ctring[] = {'A','B','C','D','E','F','G'};
    char ctringtosearchfor[] = {'D','E','F'};
    check_string_exists(ctring,ctringtosearchfor);
    return 0;
}

int check_char_exists(char fvar_string[],char fvar_chartosearchfor) {
        int fvar_hits = 0;
        for (int foo = 0; foo < (sizeof(fvar_string) / sizeof(char)); foo ++) {
            if (fvar_string[foo] == fvar_chartosearchfor) {
                fvar_hits += 1;
            }
        }
        return fvar_hits;
}

int check_string_exists(char fvar_string[], char fvar_stringtosearchfor[]) {
    int fvar_stringtosearchfor_size;
    int a;
    int fvar_string_size;
    int b;

    int subhits;
    int hits;

    //Get length of string to be searched
    while (b != NULL) {
        b = fvar_string[fvar_string_size];
        fvar_string_size++;
    }


    //Get length of string to search for
    while (a != NULL) {
        a = fvar_stringtosearchfor[fvar_stringtosearchfor_size];
        fvar_stringtosearchfor_size++;
    }

    for (int foo; foo < fvar_string_size; foo ++) {
        if (fvar_string[foo] == fvar_stringtosearchfor[0]) {
            for(int bar = 0; bar < fvar_stringtosearchfor_size;bar++) {

                //If both are equal add to subhits
                if(fvar_string[foo + bar] == fvar_stringtosearchfor[bar]) {
                    subhits +=1;
                }


                //If subhits equal to length of string to search for add 1 to hits
                if (subhits == (fvar_stringtosearchfor_size - 1)) {
                    hits += 1;
                }
            }
            subhits = 0;
        }
    } 

    printf("%d",hits);
}

EDIT: Sorry here's the output

2user@PC:~$ ./file
2user@PC:~$ ./file
2user@PC:~$ ./file
1869833334user@PC:~$ ./file
2user@PC:~$ ./file
  • 2
    You're not `return`ing anything - what did you expect to happen? – nneonneo Oct 31 '16 at 14:28
  • 2
    You are not returning anything, the variable gets assigned random memory. – SJuan76 Oct 31 '16 at 14:28
  • 2
    `fvar_string_size` isn't initialized anywhere – r3mainer Oct 31 '16 at 14:29
  • @nneonneo Thanks! –  Oct 31 '16 at 14:31
  • 2
    Compile with warnings enabled and treat the as errors. – Jabberwocky Oct 31 '16 at 14:32
  • The core problem is that you can't use sizeof on an array parameter inside a function, see the linked duplicate. Pass the size as a parameter instead. – Lundin Oct 31 '16 at 14:35
  • 1
    @Lundin Another core problem is, that `hits` and `subhits` are not initialized. I'm not sure if closing as duplicate of that is appropriate, when it is just one one problem in this code. "Too Broad" might be a better reason. – hyde Oct 31 '16 at 14:37
  • @AdrianLew I suggest you compile your code with most warnings enabled (something like `-Wall -Wextra -O3` for *gcc* and *clang* to get a pretty comprehensive list of things that are almost certainly wrong, for other compilers google yourself for how to enable warnings). Then fix all warnings you get! Then you might want to ask again, if you still have some (now assuredly different) problem with the code, which you can't solve. – hyde Oct 31 '16 at 14:39
  • You don't have any strings anywhere (except for a printf format). A string is a *null-terminated* character array. A string that contains characters A, B, C, D, E, F and G is spelled `"ABCDEFG"`. `{'A','B','C','D','E','F','G'}` is not a string. – n. m. could be an AI Oct 31 '16 at 14:40
  • @hyde Thanks, I'm new here, I'll keep that in mind next time I post stuff –  Oct 31 '16 at 14:40
  • This is not really a duplicate because there are more issues than just the sizeof problem. – Jabberwocky Oct 31 '16 at 14:57

0 Answers0