How to find substring(upper or lower) in a Sentance?

Assume I am trying to find "Hi" in a sentance. This "Hi" could be "Hi","hi","hI" or "HI".
What is the easiest or the most efficient way to find them?
I saw a post about turning everything lower or upper case. Can I avoid that?
If not please give me a very simple code.
And please share any code to satisfy my work here.
Thanks in Advance.
Last edited on
using std::regex_constants::icase for case-insensitive searches:
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>
#include <regex>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> hiCombos{"HiWorld", "hiWorld", "hIWorld", "HIWorld", "helloWorld"};
    std::regex re("hi", std::regex_constants::icase);
    //http://en.cppreference.com/w/cpp/regex/syntax_option_type
    for (size_t i = 0; i < hiCombos.size(); ++i)
    {
         bool match{false};
        std::sregex_iterator it(hiCombos[i].cbegin(), hiCombos[i].cend(), re);
        std::sregex_iterator reg_end;
        for (; it != reg_end; ++it)
    //http://www.informit.com/articles/article.aspx?p=2064649&seqNum=5
    {
        std::cout << "Substring found in string: " << i + 1 << " ";
        std::cout << it->str() << ", Position: ";
        std::cout << it->position() << std::endl;
        match = true;
    }
    if(!match)std::cout << "Not found in string " << i + 1 << "\n";
    }
}
I just wanted a simple code for just finding the substring..This is too complex for a beginner like me.
> I saw a post about turning everything lower or upper case. Can I avoid that?

We do not have to modify the original strings; we can work with copies. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <cctype>

// return a string with everything in lower case
// passed by value; so the original string is not modified
std::string to_lower( std::string str )
{ for( char& c : str ) c = std::tolower(c) ; return str ; }

// returns position where found; returns std::string::npos if not found
std::size_t find_nocase( const std::string& str, const std::string& substr )
{ return to_lower(str).find( to_lower(substr) ) ; }

// return true if str contains substr 
bool contains_nocase( const std::string& str, const std::string& substr )
{ return find_nocase( str, substr ) != std::string::npos ; }
Try this:
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
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

size_t SubStr(const string& haystack, const string& needle, bool nocase = true)
{
   if(haystack.empty() || needle.empty())
    return string::npos; // maybe better to throw exception ?

  if (!nocase)
    return haystack.find(needle);

  string haystackLower(haystack), needleLower(needle);
  transform(haystack.begin(), haystack.end(), haystackLower.begin(), tolower);
  transform(needle.begin(), needle.end(), needleLower.begin(), tolower);

  return haystackLower.find(needleLower);
}

int main()
{
  string txt("Hello World");

  size_t pos = SubStr(txt, "world");

  if (pos != string::npos)
    cout << "Found at pos: " << pos;
  else
    cout << "Couldn't find it.";

  cout << "\n\n";
  system("pause");
  return 0;
}
Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

bool findHi( string text )
{
   for ( int i = 0; i < text.size() - 1; i++ )
   {
      if ( ( text[i] == 'h' || text[i] == 'H' ) && ( text[i+1] == 'i' || text[i+1] == 'I' ) ) return true;
   }
   return false;
}

int main()
{
   string text;

   cout << "Enter some text: ";
   getline( cin, text );

   if ( findHi( text ) ) cout << "I've found the hiding hi!";
   else                  cout << "Boo; hi isn't there";
}

Enter some text: Bonjour
Boo; hi isn't there

Enter some text: HIYA!
I've found the hiding hi!



Bit more useful (position counts from 0) ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

bool equalsIgnoreCase( char a, char b ) { return tolower( a ) == tolower ( b ); }        // compares characters, ignoring case


int main()
{
   string sentence, sub;
   cout << "Enter a sentence: "           ;   getline( cin, sentence );
   cout << "Enter substring to be found: ";   getline( cin, sub      );

   int pos = search( sentence.begin(), sentence.end(), sub.begin(), sub.end(), equalsIgnoreCase ) - sentence.begin();
   if ( pos < sentence.size() ) cout << "Substring found at position " << pos;
   else                         cout << "Substring not found";
}

Enter a sentence: This was the noblest Roman of them all.
Enter substring to be found: HI
Substring found at position 1

Topic archived. No new replies allowed.