Find char sequence inside char array

Hello i am trying to make a program witch will find a sequence of chars inside char array, but my program don't work correctly need help.
https://www.upsers.mobi/
Here is my code:

#include <iostream>

int main()
{
char MyCharArray[] = "mna89ybchellomas09as9";
char Key[] = "hello";

unsigned int KeyNumb = 0;
unsigned int CorrectInRow = 0;

for(unsigned int i = 0; i < sizeof(MyCharArray); i++){
if(MyCharArray[i] == Key[KeyNumb]){ // Check if char inside MyCharArray match with first char inside Key.
CorrectInRow++; // Increment this because i want to know how many chars match in a row.
KeyNumb++; // Increment this because now i know that next time in this loop i will want to check the next char inside Key[].
if(CorrectInRow == sizeof(Key)){ // if CorrectInRow match sizeof(Key) then i know that i have a match inside MyCharArray.
std::cout << "Found: " << Key << " At: " << i << std::endl; // Print out where and what i found.
break; // Close the for loop.
}
}
else{
KeyNumb = 0; // Setting this to zero because if MyCharArray[i] did not match Key[KeyNumb] and next time i want to check the first char inside Key[].
CorrectInRow = 0; // Setting this to zero because MyCharArray[i] did not match Key[KeyNumb].
}
}
return 0;
}
Last edited on
Please edit to include code tags
https://www.cplusplus.com/articles/jEywvCM9/
You really should use the code format tags, code's pretty much unreadable without it.
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
#include <iostream>

int main()
{
    char MyCharArray[] = "mna89ybchellomas09as9";
    char Key[] = "hello";

    unsigned int KeyNumb = 0;
    unsigned int CorrectInRow = 0;

    for (unsigned int i = 0; i < sizeof(MyCharArray); i++) {
        if (MyCharArray[i] == Key[KeyNumb]) { // Check if char inside MyCharArray match with first char inside Key.
            CorrectInRow++; // Increment this because i want to know how many chars match in a row.
            KeyNumb++; // Increment this because now i know that next time in this loop i will want to check the next char inside Key[].
            if (CorrectInRow == sizeof(Key)) { // if CorrectInRow match sizeof(Key) then i know that i have a match inside MyCharArray.
                std::cout << "Found: " << Key << " At: " << i << std::endl; // Print out where and what i found.
                break; // Close the for loop.
            }
        }
        else {
            KeyNumb = 0; // Setting this to zero because if MyCharArray[i] did not match Key[KeyNumb] and next time i want to check the first char inside Key[].
            CorrectInRow = 0; // Setting this to zero because MyCharArray[i] did not match Key[KeyNumb].
        }
    }
    return 0;
}


You should remember that a char array initialised in this way also includes the null terminator at the end. So your checks shoulldn'b be against size(string), it should be against sizeof(string) - 1

If you run your code in a debugger, and step thru each line, you've be able to verify that the code is doing what you think it's doing.
Registered users can post here. Sign in or register to post.