C++ c-string substring problem

Problem:

Implement the following functions. Each function deals with null terminated C-Style strings. You can assume that any char array passed into the functions will contain null terminated data. Place all of the functions in a single file and then create a main() function that tests the functions thoroughly.

Note: You may not use any c-string functions other than strlen().

I am having trouble with the fourth function. The desired behavior is: This function returns the index in string s where the substring can first be found. For example if s is "Skyscraper" and substring is "ysc" the function would return 2. It should return -1 if the substring does not appear in the string.
prototype:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

prototype:

int findSubstring(char *str, char substring[]);

version 1:

int findSubstring(char *str, char substring[]){

int subS = -1, index1 = 0, index2 = 0;
int length1 = (strlen(str) - 1);
int length2 = (strlen(substring) - 1);

if(length1 > length2){
    for(int i = 0; i <= length2; i++){

        for(int j = 0; j <= length1; j++){

        if(*(substring + i) == *(str + j) && *(substring +i) != '\0' ){

            i++;

            if(index1 == 0){

            index1 = i;

            }
        }
            if( *(substring + i) == '\0'){
                subS = i + 2;
            }


            }

            }


        }
if (length1 < length2){
    cout << "Invalid, substring exceeds size of string!" << endl;
}
return subS;

 }

version 2:

 int findSubstring(char *str, char substring[]){
int index = -1;
int lengthStr = (strlen(str) - 1);
int lengthSub = (strlen(substring) - 1);

if (lengthStr < lengthSub){
    cout << "Invalid input, substring exceeds size of string!" << endl;
}
if( lengthSub == 0){
    cout << "";
}

if (lengthStr > lengthSub){
    for(int i = 0; i <= lengthSub; i++){
        for(int j = 0; j <= lengthStr; j++){



}




return index;
 }


neither currently works. Any help would make me jump for joy! :)
Last edited on
closed account (E3h7X9L8)
1
2
3
4
if(strstr(string,subString)) // for char array
    {
        cout <<"ok!";
    }


1
2
3
4
if(string.find(subString)) // for string
    {
        cout <<"ok!";
    }
Last edited on
Note: You may not use any c-string functions other than strlen().


Use substring[i] instead of *(substring+i). They mean the same thing but the former is more common and thus more readable.

I don't understand how your algorithm is supposed to work. Basically you want to go through str from left to right checking if the substring matches at each position.
1
2
3
4
5
6
size_t strLen = strlen(str);
size_t substrLen = strlen(substr);
for (i = 0; i < strLen-substrLen; ++i) {
    // if the first substrLen chars of str starting from position i match substring then return i
}
return -1

You can't use C string functions, but take a look at memcmp(). This function compares blocks of memory. You can use it to the code inside the loop.
Topic archived. No new replies allowed.