Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    RICERCA DI STINGHE IN C++ (vi metto alla prova)

    Ciao a tutti,
    non lasciatevi confondere dal titolo non so nemmeno io come risolvere il problema

    Allora io ho un file di testo gigantesco che contine (dopo dei caratteri a caso) queste stringhe

    <inizio>blablabla<fine>

    io devo estrarre dal file tutti i "blablabla" cioè tutte quelle parole comprese tra <inizio> e <fine> (andrebbe bene anceh con <inizio> e <fine> inclusi...

    MA COME SI FA?!?!?! STO IMPAZZENDO E' TUTTO IL GIORNO CHE CI PENSO!!!!!!!

    HELP!!!

  2. #2
    Oggi è il tuo giorno fortunato, l'ho già scritta io questa funzione.

    Ricordami domani che te la posto (ce l'ho a casa)

  3. #3

    !!!!!!!!!!!!!! DAVVERO!?

    Davvero?!

    Avevo perso le speranze oramai!!!

    Ti ringrazio molto!!! ))

  4. #4
    codice:
    string find_pat(string where, int * pos, string start, string end)
    {
    	int pos2, pos3;
    	int work=1;
    
    	pos2=*pos;
    	while(work)
    	{
    		pos2 = where.find (end, pos2);
    		if(pos2 == string::npos) return string("");
    	
    		pos3 = where.rfind (start, pos2);
    		if(pos3 == string::npos || pos3 < *pos) 
    		{
    			pos2 = pos2 + end.length() ;
    		}
    		else
    		{
    			pos3 += start.length();
    			work=0;
    		}
    	}
    
    	*pos=pos2+end.length();
    	return where.substr (pos3, pos2-pos3);
    }
    usala cosi'
    codice:
    int pos = 0;
    string s = find_pat("Salve, io sono una stringa", &pos, string("Salve,"), "sono una stringa");
    cout << s;

  5. #5

    perdona la mia ignoranza ...

    ... ma quale dei due codici devo usare?

    e poi mi dice undefine simbol "string" (?).

    Percaso è per VC++?

    Grazie AL !

  6. #6
    ESEMPIO:
    codice:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string find_pat(string where, int * pos, string start, string end);
    
    int main()
    {
       int pos=0;
       string t;
       string s = "<inizio>...</fine>bla bla bla <inizio>\?\?\?</fine>";
       while((t=find_pat(s, &pos, "<inizio>", "</fine>")) != "")
           cout << t << endl;
       return 0;
    }
    
    string find_pat(string where, int * pos, string start, string end)
    {
    	int pos2, pos3;
    	int work=1;
    
    	pos2=*pos;
    	while(work)
    	{
    		pos2 = where.find (end, pos2);
    		if(pos2 == string::npos) return string("");
    	
    		pos3 = where.rfind (start, pos2);
    		if(pos3 == string::npos || pos3 < *pos) 
    		{
    			pos2 = pos2 + end.length() ;
    		}
    		else
    		{
    			pos3 += start.length();
    			work=0;
    		}
    	}
    
    	*pos=pos2+end.length();
    	return where.substr (pos3, pos2-pos3);
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.