Reading every string in program

How to read every string in a visual studio program? I need to read all of them, preferably at the same time, searching for a certian phrase. Something like:


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
string phrase1 = "Hello World!";
string phrase2 = "Hello World";
string correctPhrase;

if (phrase1 || phrase2 == "Hello World!") {
cout << "Phrase " << correctPhrase << " has Hello World!";
}
}


btw, i need to this x10.
Last edited on
1
2
3
4
5
6
std::string word;
while (std::cin >> word){ //getline(std::cin, word) if you want spaces
   if (word == some_string){
      //...
   }
}



> i need to this x10.
¿?
Last edited on
i need to read 10 different strings
This simplified example won't take care of raw string literals, won't concatenate adjacent strings, and ignores preprocessor directives like #include #ifdef etc. It also won't handle strings that span across multiple lines (std::regex_constants::multiline is not particularly well supported)

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
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <fstream>

// get quoted strings in a line of text
std::vector<std::string> get_quoted_strings( const std::string& line )
{
    // an opening quote character
    // zero or more occurrences of either "any non-quote non-backslash character" or "a backslash followed by any character"
    // a closing quote character
    static const std::regex quoted_string_re( R"("(?:[^"\\]|\\.)*")" ) ;
    using iterator = std::sregex_iterator ;
    static const iterator end ;

    std::vector<std::string> result ;
    for( iterator iter( line.begin(), line.end(), quoted_string_re ) ; iter != end ; ++iter )
        result.push_back( iter->str() ) ;
    return result ;
}

// get quoted strings in all lines
std::vector<std::string> get_quoted_strings( std::istream& stm )
{
    std::vector<std::string> result ;

    std::string line ;
    while( std::getline( stm, line ) )
        for( const auto& str : get_quoted_strings(line) ) result.push_back(str) ;

    return result ;
}

int main()
{
    const std::string str = "string #one" "string number two" "and a string three" ;
    "this is a long fourth string" "finally, a fifth string containing \"escaped\" quotes" ;
    "a line with a couple of " "" "empty" "" "strings." ;

    std::ifstream this_file(__FILE__) ;
    for( const std::string& str : get_quoted_strings(this_file) ) std::cout << str << '\n' ;
}

https://rextester.com/ILEBN40637
Topic archived. No new replies allowed.