File handling in C ++....

How to search form insensitive to case sensitive?

I'm trying to emulate the behavior of the function "grep" in Unix. Novice programming

Note: I do not speak very well English.

#include <iostream>
#include <fstream>
#include<string>
#include<vector>
#include <ctype.h>

using namespace std;

int main(int argc, char** argv)
{

const unsigned int CANT_ARGUMENTOS = 4;
string palabraBuscar = argv[1];
string opcion = argv[2];
ifstream archivo(argv[3]);
vector <string>cadena;
string linea;
int encontrado = 0;
int x = 0;
int cont = 0;



if(argc != CANT_ARGUMENTOS)
{
cout << "Numero incorrecto de argumentos!" << endl;
}
else if(archivo.fail())
{
cout << "El archivo no existe!" << endl;
}
else if(opcion == "-c" || opcion == "-C")
{

while(getline(archivo,linea))
{

cadena.push_back(linea);
}

for(int i = 0; i < cadena.size(); i++)
{


encontrado = cadena.at(i).find(palabraBuscar, x);

if (encontrado != string::npos)
{
x = encontrado + 1;
i--;
cont++;

}
else
{
x = 0;
}
}cout << cont;



}
else if(opcion == "-i" || opcion == "-I")
{
cout << "En proceso" << endl;
}
else
{
cout << "Error, la opcion '" << opcion << "' no es valida!" << endl;
}

return 0;


}



Last edited on
The algorithm std::search() http://en.cppreference.com/w/cpp/algorithm/search
searches for the occurrence of a subsequence in a range.

We can use the predicate version of this algorithm to specify that character comparisons are to be case-insensitive.

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
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
    std::string linea = "How now BROWN cow." ;
    std::string palabraBuscar = "brown" ;

    // case insentive search
    auto result = std::search( linea.begin(), linea.end(), // search in this sequence of characters

                               palabraBuscar.begin(), palabraBuscar.end(), // for this subsequence

                               [] ( char a, char b ) { return std::toupper(a) == std::toupper(b) ; }
                                // ignoring case while comparing characters for equality
                               ) ;

    if( result != linea.end() ) // if the susequence was found
    {
        std::cout << "found: '" << palabraBuscar << "' starting at position " << result - linea.begin()
                  << " in line: '" << linea << "'\n" ;
    }
}

http://coliru.stacked-crooked.com/a/3e00fb08441e3c34
Sorry, but could you be more specific, please; I'm a beginner. Thank you.
Last edited on
The begin() and end() member functions of std::string yield iterators that identify the range of characters held by the string.

Read up on iterators: http://www.cs.helsinki.fi/u/tpkarkka/alglib/k06/lectures/iterators.html
http://www.mochima.com/tutorials/STL.html


std::search() is a standard algorithm.
http://www.mochima.com/tutorials/STL_algorithms.html


This [] ( char a, char b ) { return std::toupper(a) == std::toupper(b) ; } is a lambda expression.
http://stackoverflow.com/a/7627218
Topic archived. No new replies allowed.