some help with find cmd..

hi! i need some help with the find cmd, i tryed to test the find command for one of my projects, a siri cind of thing, so i want to know how to detect spacific words in strings, heres my coding!
HELLLP!!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
	string mlg = "abcd";
	string lol = "d";
if (mlg.find(lol)) {
    cout << "found the letter " << lol << endl;
}
else {
	cout << "did not find the letter";
}
		return 0;
	}

HEEEELLLLLPPP!!!!!
Last edited on
std::string::find returns the position of the substring or character if it is found,
otherwise it returns std::string::npos

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

int main() {

    const std::string mlg = "abcd efgh ijkl";
    std::cout << "string: " << std::quoted(mlg) << '\n' ;

    const std::string lol = "d efg";
    const auto pos = mlg.find(lol) ;
    if( pos != std::string::npos )
        std::cout << "found the substring " << std::quoted(lol) << " starting at position " << pos << '\n' ;
    else std::cout << "did not find the substring\n" ;

    if( mlg.find( 'd' ) != std::string::npos ) std::cout << "found character 'd'\n" ;
}
@JLBorges
it says quoted is not a member of std??? wat???
The manipulator std::quoted came with C++14; if you are using a version of C++ prior to that,
try this (quote manually):

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

int main() {

    const std::string mlg = "abcd efgh ijkl";
    std::cout << "string: \"" << mlg << "\"\n" ;

    const std::string lol = "d efg";
    const auto pos = mlg.find(lol) ;
    if( pos != std::string::npos )
        std::cout << "found the substring \"" << lol << "\" starting at position " << pos << '\n' ;
    else std::cout << "did not find the substring\n" ;

    if( mlg.find( 'd' ) != std::string::npos ) std::cout << "found character 'd'\n" ;
}

To read a complete line (which may contain embedded spaces) as a single string, use std::getline()

For example:
1
2
3
std::string str ;
std::getline( std::cin, str ) ;
std::cout << str << '\n' ;
Last edited on
thanks ;D
Topic archived. No new replies allowed.