A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d3/d7d/brute__force__string__searching_8cpp.html below:

TheAlgorithms/C++: strings/brute_force_string_searching.cpp File Reference

Loading...

Searching...

No Matches

String pattern search - brute force. More...

#include <iostream>
#include <cstring>
#include <vector>

Go to the source code of this file.

const std::vector< std::vector< std::string > >  test_set

String pattern search - brute force.

Definition in file brute_force_string_searching.cpp.

◆ brute_force() int string_search::brute_force ( const std::string & text, const std::string & pattern )

Find a pattern in a string by comparing the pattern to every substring.

Parameters
text Any string that might contain the pattern. pattern String that we are searching for.
Returns
Index where the pattern starts in the text
-1 if the pattern was not found.

Definition at line 21 of file brute_force_string_searching.cpp.

21 {

22 size_t pat_l = pattern.length();

23 size_t txt_l = text.length();

24 int index = -1;

25 if (pat_l <= txt_l) {

26 for (size_t i = 0; i < txt_l - pat_l + 1; i++) {

27 std::string s = text.substr(i, pat_l);

28 if (s == pattern) {

29 index = i;

30 break;

31 }

32 }

33 }

34 return index;

35}

◆ main()

Main function

Definition at line 47 of file brute_force_string_searching.cpp.

47 {

50

51 if (std::to_string(output) == i[2]) {

52 std::cout << "success\n";

53 } else {

54 std::cout << "failure\n";

55 }

56 }

57 return 0;

58}

const std::vector< std::vector< std::string > > test_set

int brute_force(const std::string &text, const std::string &pattern)

◆ test_set const std::vector<std::vector<std::string> > test_set Initial value:

= {

{"a", "aa", "-1"}, {"a", "a", "0"}, {"ba", "b", "0"},

{"bba", "bb", "0"}, {"bbca", "c", "2"}, {"ab", "b", "1"}}

set of test cases

Definition at line 41 of file brute_force_string_searching.cpp.

41 {

42

43 {"a", "aa", "-1"}, {"a", "a", "0"}, {"ba", "b", "0"},

44 {"bba", "bb", "0"}, {"bbca", "c", "2"}, {"ab", "b", "1"}};


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4