hi guys,
heres the problem. i have an assignment to change certain letters or combination of letters into some other letters or just to make them dissapier. Say like.. "the" to>> "ze", where "th" is replaced with "z". the program is consists of functions like these. checking two letters forward is fine.. iam using an ITERATOR to move-about

but the problem starts here...going back wards 3 nodes...
i have to make a way to get rid of the last "e" on words..but only on words of more than 3 letters.
say like: "here" would be "her"....
"bre" would still be "bre"..."ze" would still be "ze"

>the way i'm trying to do is using a while loop and checking if words are more than 3 letters long....if they are i apply changes....

>if i come accross nodes other than "alphabetical" (like a "space")within 3 nodes back the loop breaks..do nothing..change nothing...

please refer to code :)

void ReplaceE(list <char> &myList)

{int count = 0;
	list<char>::iterator itr;          //main iterator
	list<char>::iterator it;           //another iterator
   for (itr = myList.begin(); itr != myList.end(); itr++ ) 
	   
if ((tolower(*itr) == 'e') )     // check for an "e"
{
	itr++;
	if((*itr) == ' ')    // if the next node is a space countinue following
	{
	itr--;        //on 'e'
	it = itr;    //assign the value to another iterator
	
	while(count != 3)
	{	
		itr--;
		if(isalpha(*itr))       //check if passing "abc"s
		     {
		count ++;
		      }
		else
		break;         // if come accross non "abc" break
               }

	
	if (count == 3)     //   assumed never happens if word is only 3 ltrs
                {
	*it =' ';      //change the node("e") to a " "
	}
}
}
}

but it doesnt work. even the 2 letter worrds get affected. like "ze" becomes "z". lol i have tried so may variations of the "while loop" changing the variables with out any luck. some times the program gets aborted :(. ( at times it says "list iterator not incrementable")

--(i am using myList.unique() to get rid of the double charactors which get left by the above functions..not good but for the time being..)--

Please if you could point me what i am doing wrong here. i would much apriciate it. have been doing this allnight hehe.

thanks guys.
yaka.

> the way i'm trying to do is using a while loop and checking if words are more than 3 letters
> long....if they are i apply changes....
> if i come accross nodes other than "alphabetical" (like a "space")within 3 nodes back the loop
> breaks..do nothing..change nothing...
the logic looks ok. erasing the character (instead of replacing it with a space) would eliminate the need to remove duplicate spaces later.

> at times it says "list iterator not incrementable"
you need to check for incrementing past end of sequence after *every* increment.

> but it doesnt work. even the 2 letter worrds get affected
you are initializing count just once. you need to reset it for every word.

using a std::string (or a container with a random_access iterator like std::vector ) would have made the code simpler.

#include <algorithm>
#include <functional>

template< typename CNTR > 
void replace_char( CNTR& cntr, char e = 'e', std::size_t min_sz = 4 )
{
  using namespace std ;
  typedef typename CNTR::iterator iterator ;
  const char space = ' ' ;
  // TODO: this example assumes that words are seperated by spaces
  // TODO: modify to use std::isalpha, std::isspace etc.

  iterator first = find_if( cntr.begin(), cntr.end(),
                            bind2nd( not_equal_to<char>(), space ) ) ;
  while( first != cntr.end() )
  {
    iterator last = first ;
    typename CNTR::size_type n = 0 ;
    while( (last!=cntr.end()) && (*last!=space) ) { ++last ; ++n ; }
    if( n > min_sz )
    {
     iterator prev = last ; --prev ;
     if( *prev == e ) last = cntr.erase(prev) ;
    }
    first = find_if( last, cntr.end(),
                     bind2nd( not_equal_to<char>(), space ) ) ;
  }
}

// small test case
#include <string>
#include <iostream>
#include <list>
#include <iterator>

int main()
{
  std::string str = " 12e 2123e 31234x 4e 52e 61234567e 71234y 81e" ;
  std::list<char> lst( str.begin(), str.end() ) ;
  std::cout << str << '\n' ;
  replace_char( str ) ;
  std::cout << str << '\n' ;
  replace_char( lst ) ;
  std::copy( lst.begin(), lst.end(), 
            std::ostream_iterator<char>(std::cout) ) ;
  std::cout << '\n' ;
}

thanks bhai, hmm i am not supose to use vectors in this case :(

hi guys,
i have another problem. im trying to eliminate double charactors in a list of charactors ( each charactor is stored as a separate node)

i tried doing this;

int main ()
{
  list<char> myList;   // this is a template right? 
  char *file = "file.txt";
ReplaceDouble(myList);

.......

void ReplaceDouble(list <char> &myList)
{
list<char>::iterator itr,itx;
for (itr = myList.begin(); itr != myList.end(); itr++ ) 
if (isalpha(*itr)= isalpha(*++itr))
{
	itr--;
	itx = itr;
	myList.erase(itr);
	itr = itx;
}
}

but it doesnt work. i have got this approch to work in some other cases but for the 'double charactors' it doesnt work.

can anyone plese tell me how i can fix this, thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.