0

I want to create a programm, that counts how much time one array is contained in another one.

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cmath>
#include <list>
using namespace std;

int func(char one[],char two[]){
    int result = 0;
    int length_one = sizeof(one)/sizeof(*one) - 1;
    int length_two = sizeof(two)/sizeof(*two) - 1;
    for(int i = 0;i<length_one;++i){
        int temp = 0;
        if(one[i] != two[0]) continue;
        else{
            for(int j = 0;j<length_two;++j){
                if(two[j] != one[j+i])break;
                else{
                    temp+=1;
                }
            }
            if(temp == length_two){
                result +=1;
            }
        }

    }
    return result;
}


int main(){
    char one[] = "Hello everybody";
    char two[]= "every";
    cout << func(one,two);

}

I checked separately the line: int length_two = sizeof(two)/sizeof(*two) - 1; the size is equal to 5. However in my code int length_two is equal to 7. How is it possible?

Rob_Fir
  • 155
  • 1
  • 7

0 Answers0