View Single Post
Old 10-12-2005, 05:00 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
For a true palidrom, this would work:
Code:
#include <iostream>
#include <stack>

int main()
{
  std::string INput; /* normal input */
  std::string inPUT; /* altered input */
  std::stack <char> STak;   /* normal stack */
  std::stack <char> stAK;   /* altered stack */
  char ptr;
  std::cout << "Please enter a line: ";
  std::cout.flush();
  std::getline(std::cin, INput);
  /* convert the line */
  
  for(unsigned int i=0; i < INput.size(); ++i)
    {
      if(INput[i] >= 'A' && INput[i] <= 'Z')
	inPUT += (INput[i] | 32);
      else
	if(INput[i] >= 'a' && INput[i] <= 'z')
	  inPUT += (INput[i] & (~32));
	else
	  inPUT += INput[i];
      /* push onto first stack */
      STak.push(INput[i]);
    }
  /* push converted line _reversed_ onto second stack */
  for(int i=inPUT.size()-1; i >= 0; i--)
      stAK.push(inPUT[i]);
    
  /* the stacks will allways be of same size */
  while(stAK.size()>1&& (stAK.top() == STak.top()))
    { /*just pop those stacks */
      stAK.pop();
      STak.pop();
   }
  std::cout << inPUT << std::endl;
  std::cout << INput << std::endl;
  std::cout << "Is " << (stAK.size()>1? "not ":"") << "a palidrom" << std::endl;
  return 0;
}
But since your example is showing a "i am AM I" as beeing a palidrom, hence am <> AM isn't a true palidrom, but we'll handle it anyway..
this would do it:
Code:
#include <iostream>
#include <stack>
#include <string>
#include <vector>

int main()
{
  std::string INput; /* normal input */
  std::string inPUT; /* altered input */
  std::string temp;
  std::vector <std::string> words;
  std::stack <char> STak;   /* normal stack */
  std::stack <char> stAK;   /* altered stack */
  char *ptr;
  std::cout << "Please enter a line: ";
  std::cout.flush();
  std::getline(std::cin, INput);
  /* convert the line */
  
  for(unsigned int i=0; i < INput.size(); ++i)
    {
      if(INput[i] >= 'A' && INput[i] <= 'Z')
	inPUT += (INput[i] | 32);
      else
	if(INput[i] >= 'a' && INput[i] <= 'z')
	  inPUT += (INput[i] & (~32));
	else
	  inPUT += INput[i];
      /* push onto first stack */
      STak.push(INput[i]);
    }
  /* align the input line wordwise */
  temp=inPUT.substr(0);
  /* we assume they are using space between each words */
  ptr = strtok ((char*) temp.c_str()," ");
  while (ptr != NULL)
  {
    words.push_back(ptr);
    ptr = strtok (NULL, " ");
  }

  /* push converted line _reversed_wordwise_ onto second stack */
  for(int i=words.size()-1; i >= 0; i--)
    {
      for(unsigned int j=0; j < words[i].size(); ++j)
	  stAK.push(words[i][j]);
      if(i>0)
	stAK.push(' ');
    }
  /* the stacks will allways be of same size */
  while(stAK.size() > 1 && (STak.top() == stAK.top()))
    { /*just pop those stacks */
      stAK.pop();
      STak.pop();
    }
  std::cout << inPUT << std::endl;
  std::cout << INput << std::endl;
  std::cout << "Is " << (stAK.size()>1? "not ":"") << "a palidrom" << std::endl;
  return 0;
}
edit: just realised you should use a queue aswell, you could just change the second stack to beeing a queue, since C++ provides you with a queue class, I dont see why you would make your own..

Only change would be, that you ask for empty() uppon if it is or not a palidrom and you ask for first() instead of top(), something like this for the true palidrom program:
Code:
#include <iostream>
#include <stack>
#include <queue>
#include <string>

int main()
{
  std::string input; /* normal input */
  std::string INPUT; /* altered input */
  std::stack <char> stack;   /* normal stack */
  std::queue <char> queue;   /* altered stack */

  std::cout << "Please enter a line: ";
  std::cout.flush();
  std::getline(std::cin, input);
  /* convert the line */
  
  /* push onto stack and queue*/
  for(unsigned int i=0; i < input.size(); ++i)
    {
      if(input[i] >= 'A' && input[i] <= 'Z')
	queue.push(input[i] | 32);
      else
	if(input[i] >= 'a' && input[i] <= 'z')
	    queue.push(input[i] & (~32));
	else
	   queue.push(input[i]);
      stack.push(input[i]);
      INPUT += queue.back();
    }


  while(!queue.empty() && (stack.top() == queue.front()))
    { /*just pop those stacks/queues */
      stack.pop();
      queue.pop();
    }
  std::cout << input << std::endl;
  std::cout << INPUT << std::endl;
  std::cout << "Is " << (queue.empty()? "":"not ") << "a palidrom" << std::endl;
  return 0;
}
But since we still have to take advantage of the word like palidrom way, then this would do:
Code:
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include <vector>

int main()
{
  std::string input; /* normal input */
  std::string INPUT; /* altered input */
  std::string temp;
  std::stack <char> stack;   /* normal stack */
  std::queue <char> queue;   /* altered stack */
  std::vector <std::string> words;
  char *ptr;
  std::cout << "Please enter a line: ";
  std::cout.flush();
  std::getline(std::cin, input);
  /* convert the line */
  
  /* push onto stack and queue*/
  for(unsigned int i=0; i < input.size(); ++i)
    {
      if(input[i] >= 'A' && input[i] <= 'Z')
	INPUT += (input[i] | 32);
      else
	if(input[i] >= 'a' && input[i] <= 'z')
	    INPUT += (input[i] & (~32));
	else
	   INPUT += input[i];
      stack.push(input[i]);
    }
  temp=INPUT.substr(0);
  /* we assume they are using space between each words */
  ptr = strtok ((char*) temp.c_str()," ");
  while (ptr != NULL)
  {
    words.push_back(ptr);
    ptr = strtok (NULL, " ");
  }
 /* push converted line _wordwise_reverted_ onto queue */
  for(unsigned int i=0; i<words.size(); ++i)
    {
      for(int j=words[i].size()-1; j >=0; j--)
	  queue.push(words[i][j]);
      if(i<words.size()-1)
	queue.push(' ');
    }
  while(!queue.empty() && (stack.top() == queue.front()))
    { /*just pop those stacks/queues */
      stack.pop();
      queue.pop();
    }
  std::cout << input << std::endl;
  std::cout << INPUT << std::endl;
  std::cout << "Is " << (queue.empty()? "":"not ") << "a palidrom" << std::endl;
  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 10-12-2005 at 05:53 AM.
redhead is offline   Reply With Quote