944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 15686
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 10th, 2005
0

Help on stack , queue, palindrome program...

Expand Post »
Question:--
Write a program that read a line of text, changes each uppercase letter to lowercase and places each letter both in a queue and onto a stack.
The program should then verify whether the line of text is a palindrome


Output:

Please enter a line of text
I am A.I


i AM a.i

This is a palindrome

---------------------------------------------------------------------

How do i change the capital letter to smaller letter as above output example??? wat function i should add??
i using fstream function..now..wat should i do to change which i can enter text by my own??

thanks 4 helping..


My answers below:-

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS
  3. #include <fstream>
  4. #include<string>
  5. using namespace std;
  6. const int MAX=50; // initialize max string size of 50 characters
  7. typedef char StackElement; // define StackElement
  8. typedef char QueueElement; // define QueueElement
  9. class Stack
  10. {
  11. public:
  12. Stack(){top=-1;arr[MAX]=0;} // default stack constructor
  13. void push(StackElement & ch); // push function
  14. StackElement topNpop(); // top and pop functions combined
  15. bool empty() const; // empty function
  16. private:
  17. StackElement arr[MAX]; // define char array
  18. int top; // define int top
  19.  
  20. };
  21. /*******************************************
  22. FUNCTION: push()
  23. DESCRIPTION: Pushes an element onto the stack
  24. PRECONDITION: Waiting for function call
  25. POSTCONTION: New element character on top of stack
  26. *******************************************/
  27. inline void Stack::push(StackElement & ch)
  28. {
  29. if(top<MAX)
  30. {
  31. top++; // increment top
  32. arr[top]=ch; // push onto stack
  33. }
  34. else
  35. {
  36. cout<<"Stack is full.\n"; // display stack is full
  37. }
  38.  
  39.  
  40. }
  41. /*******************************************
  42. FUNCTION: topNpop()
  43. DESCRIPTION: Reads and pops top element off the stack
  44. PRECONDIION: Waiting for function call
  45. POSTCONDITION: One element read and removed fromt he stack
  46. RETURN: Top element from stack
  47. ********************************************/
  48.  
  49. inline StackElement Stack::topNpop()
  50. {
  51. if(top>-1)
  52. {
  53. return(arr[top]); // returns top element
  54. top--; // remove froms stack
  55. }
  56. else
  57. {
  58. cout<<"Stack is empty.\n"; // display stack is empty
  59. return(0);
  60.  
  61. }
  62.  
  63. }
  64. /*******************************************
  65. FUNCTION: empty()
  66. DESCRIPTION: returns result value if stack is empty
  67. PRECONDITION: result=false
  68. POSTCONDITION: result may be true or remain false
  69. RETURN: result if true or false
  70. ********************************************/
  71. inline bool Stack::empty() const
  72. {
  73. bool result=false; // initialize bool as false
  74. if (top==-1)
  75. {
  76. result=true; // if top is -1 return result true
  77. return(result);
  78. }
  79. else
  80. {
  81. return(result); // else return false
  82. }
  83. }
  84. class Queue // Queue class
  85. {
  86. public:
  87. Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor
  88. void addq(QueueElement & ch); // define addq
  89. QueueElement frontNremoveq(); // define frontNremove
  90. private:
  91. QueueElement arr[MAX]; // initialize QueueElement array
  92. int front, back; // initialize int front and back
  93.  
  94. };
  95. /*******************************************
  96. FUNCTION: addq()
  97. DESCRIPTION: adds an element onto the queue
  98. PRECONDITION: Waiting for element to add
  99. POSTCONDITION: New element now on the queue
  100. ********************************************/
  101. inline void Queue::addq(QueueElement &ch)
  102. {
  103. if(front!=(back+1)%MAX)
  104. {
  105. arr[back]=ch; // add element to back of queue
  106. back=(back+1)%MAX;
  107. }
  108. else
  109. {
  110. cerr<<"Error Queue is full\n"; // display queue is full
  111. }
  112. }
  113. /*******************************************
  114. FUNCTION: frontNremoveq()
  115. DESCRIPTION: reads and removes front element from queue
  116. PRECONDITION: front pointing to front of queue
  117. POSTCONDITION: front element is returned and then incremented
  118. ********************************************/
  119. inline QueueElement Queue::frontNremoveq()
  120. {
  121. if(front!=back)
  122. {
  123. return(arr[front]); // return front element
  124. front++; // remove front element
  125. }
  126. else
  127. {
  128. cout<<"Queue is empty.\n"; // display queue is empty
  129. return(0);
  130. }
  131. }
  132.  
  133. /***************************MAIN******************************/
  134. int main()
  135. {
  136. Stack S; // initialize stack
  137. Queue Q; // initialize queue
  138. string s;
  139. int i=0; // initialze int 'i'
  140. char string[MAX]; // initialize char string
  141. bool RESULT=false; // initilize bool RESULT to false
  142.  
  143.  
  144.  
  145. ifstream inside ("palindromeinput.txt");
  146. if (! inside.is_open())
  147. { cout << "Error opening file"; exit (1); }
  148.  
  149. while (! inside.eof() )
  150. {
  151. inside.getline (string,100);
  152. cout << string << endl;
  153. }
  154.  
  155. while(string[i]!=NULL)
  156. {
  157. S.push(string[i]); // push chars individually from string to
  158. Q.addq(string[i]); // stack and queue
  159. i++; // next char
  160. }
  161. while(i>0)
  162. {
  163. if(S.topNpop()==Q.frontNremoveq()) // compare each element from
  164. { // stack and queue
  165. RESULT=true; // if same for all chars return true
  166. }
  167. else
  168. {
  169. RESULT=false; // if not same for any char break and return false
  170. break;
  171. }
  172. i--;
  173. }
  174.  
  175.  
  176. if(RESULT==true)
  177. {
  178. cout<<string<<" is a palindrome\n"; // display if true
  179. }
  180. else
  181. {
  182. cout<<string<<" is not a palindrome\n"; // display if false
  183. }
  184.  
  185.  
  186.  
  187. ofstream outside ("palindromeoutput.txt");
  188. if (outside.is_open())
  189. {
  190.  
  191. outside << string;
  192. outside<< string;
  193. outside.close();
  194. }
  195. return 0;
  196. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
mysterio is offline Offline
16 posts
since Sep 2005
Oct 10th, 2005
0

Re: Help on stack , queue, palindrome program...

Quote ...
How do i change the capital letter to smaller letter as above output example
use tolower() to convert a character to lower-case. It only works on one character, so if you want to convert a whole string to lower case you have to create a loop to iterate each character. Here is a c++ example.
C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cctype>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string str = "UPPER-CASE";
  11. transform(str.begin(),str.end(),str.begin(),tolower);
  12. cout << str << endl;
  13. return 0;
  14. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 10th, 2005
0

Re: Help on stack , queue, palindrome program...

I'm not terribly STL-savvy, what is this error telling me?
C++ Syntax (Toggle Plain Text)
  1. //Error E2285 testpp.cpp 11: Could not find a match for 'transform<InputIterator1,InputIterator2,OutputIterator,BinaryOperation>(char *,char *,char *,charT (*)(charT,const locale &))' in function main()
...while I continue searching for an answer on my own...

[edit]Found this:
Quote ...
transform
template<class InIt, class OutIt, class Fn1>
    OutIt transform(InIt first, InIt last, OutIt dest,
        Fn1 func);
template<class InIt1, class InIt2, class OutIt,
    class Fn2>
    OutIt transform(InIt1 first1, InIt1 last1,
        InIt2 first2, OutIt dest, Fn2 func);
The first template function evaluates *(dest + N) = func(*(first + N)) once for each N in the range [0, last - first). It then returns dest + (last - first). The call func(*(first + N)) must not alter *(first + N).

The second template function evaluates *(dest + N) = func(*(first1 + N), *(first2 + N)) once for each N in the range [0, last1 - first1). It then returns dest + (last1 - first1). The call func(*(first1 + N), *(first2 + N)) must not alter either *(first1 + N) or *(first2 + N).
[Dani - Here I really miss the fixed fonts! Follow the dinkumware link and note my previous replies as to why. Oh, and font sizes would have been beneficial as well.]

[edit=2]Ahh.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 11th, 2005
0

Re: Help on stack , queue, palindrome program...

Quote originally posted by Dave Sinkula ...
I'm not terribly STL-savvy, what is this error telling me?
C++ Syntax (Toggle Plain Text)
  1. //Error E2285 testpp.cpp 11: Could not find a match for 'transform<InputIterator1,InputIterator2,OutputIterator,BinaryOperation>(char *,char *,char *,charT (*)(charT,const locale &))' in function main()
...while I continue searching for an answer on my own...
Dev-C++ (gcc) has a compiler bug
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 11th, 2005
0

Re: Help on stack , queue, palindrome program...

Quote originally posted by Ancient Dragon ...
Dev-C++ (gcc) has a compiler bug
Borland must have the same bug then.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 12th, 2005
0

Re: Help on stack , queue, palindrome program...

Quote originally posted by Ancient Dragon ...
use tolower() to convert a character to lower-case. It only works on one character, so if you want to convert a whole string to lower case you have to create a loop to iterate each character. Here is a c++ example.
C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <cctype>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string str = "UPPER-CASE";
  11. transform(str.begin(),str.end(),str.begin(),tolower);
  12. cout << str << endl;
  13. return 0;
  14. }

this what i have done...
still hv some problems...

1.my output:-
Please enter a line of text:
i am not a.i
i am not a.i
This is a palindrome <-----it must be this is not a palindrome(uncorrect answers)
Press any key to continue


2. how to use the touppercase() n tolowercase()combine as below???

I am A.I

i AM a.i



C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include <cstdlib> // For EXIT_FAILURE and EXIT_SUCCESS
  3. #include <fstream>
  4. #include<string>
  5.  
  6. #include <algorithm>
  7. #include <iomanip>
  8. #include <cctype>
  9.  
  10. using namespace std;
  11.  
  12. const int MAX=50; // initialize max string size of 50 characters
  13. typedef char StackElement; // define StackElement
  14. typedef char QueueElement; // define QueueElement
  15. class Stack
  16. {
  17. public:
  18. Stack(){top=-1;arr[MAX]=0;} // default stack constructor
  19. void push(StackElement & ch); // push function
  20. StackElement topNpop(); // top and pop functions combined
  21. bool empty() const; // empty function
  22. private:
  23. StackElement arr[MAX]; // define char array
  24. int top; // define int top
  25.  
  26. };
  27. /*******************************************
  28. FUNCTION: push()
  29. DESCRIPTION: Pushes an element onto the stack
  30. PRECONDITION: Waiting for function call
  31. POSTCONTION: New element character on top of stack
  32. *******************************************/
  33. inline void Stack::push(StackElement & ch)
  34. {
  35. if(top<MAX)
  36. {
  37. top++; // increment top
  38. arr[top]=ch; // push onto stack
  39. }
  40. else
  41. {
  42. cout<<"Stack is full.\n"; // display stack is full
  43. }
  44.  
  45.  
  46. }
  47. /*******************************************
  48. FUNCTION: topNpop()
  49. DESCRIPTION: Reads and pops top element off the stack
  50. PRECONDIION: Waiting for function call
  51. POSTCONDITION: One element read and removed fromt he stack
  52. RETURN: Top element from stack
  53. ********************************************/
  54.  
  55. inline StackElement Stack::topNpop()
  56. {
  57. if(top>-1)
  58. {
  59. return(arr[top]); // returns top element
  60. top--; // remove froms stack
  61. }
  62. else
  63. {
  64. cout<<"Stack is empty.\n"; // display stack is empty
  65. return(0);
  66.  
  67. }
  68.  
  69. }
  70. /*******************************************
  71. FUNCTION: empty()
  72. DESCRIPTION: returns result value if stack is empty
  73. PRECONDITION: result=false
  74. POSTCONDITION: result may be true or remain false
  75. RETURN: result if true or false
  76. ********************************************/
  77. inline bool Stack::empty() const
  78. {
  79. bool result=false; // initialize bool as false
  80. if (top==-1)
  81. {
  82. result=true; // if top is -1 return result true
  83. return(result);
  84. }
  85. else
  86. {
  87. return(result); // else return false
  88. }
  89. }
  90. class Queue // Queue class
  91. {
  92. public:
  93. Queue(){front=0, back=0;arr[MAX]=0;} // Queue default constructor
  94. void addq(QueueElement & ch); // define addq
  95. QueueElement frontNremoveq(); // define frontNremove
  96. private:
  97. QueueElement arr[MAX]; // initialize QueueElement array
  98. int front, back; // initialize int front and back
  99.  
  100. };
  101. /*******************************************
  102. FUNCTION: addq()
  103. DESCRIPTION: adds an element onto the queue
  104. PRECONDITION: Waiting for element to add
  105. POSTCONDITION: New element now on the queue
  106. ********************************************/
  107. inline void Queue::addq(QueueElement &ch)
  108. {
  109. if(front!=(back+1)%MAX)
  110. {
  111. arr[back]=ch; // add element to back of queue
  112. back=(back+1)%MAX;
  113. }
  114. else
  115. {
  116. cerr<<"Error Queue is full\n"; // display queue is full
  117. }
  118. }
  119. /*******************************************
  120. FUNCTION: frontNremoveq()
  121. DESCRIPTION: reads and removes front element from queue
  122. PRECONDITION: front pointing to front of queue
  123. POSTCONDITION: front element is returned and then incremented
  124. ********************************************/
  125. inline QueueElement Queue::frontNremoveq()
  126. {
  127. if(front!=back)
  128. {
  129. return(arr[front]); // return front element
  130. front++; // remove front element
  131. }
  132. else
  133. {
  134. cout<<"Queue is empty.\n"; // display queue is empty
  135. return(0);
  136. }
  137. }
  138.  
  139. /***************************MAIN******************************/
  140. int main()
  141. {
  142. Stack S; // initialize stack
  143. Queue Q; // initialize queue
  144. string s ;
  145. int i=0; // initialze int 'i'
  146. char string[MAX]; // initialize char string
  147. bool RESULT=false; // initilize bool RESULT to false
  148.  
  149.  
  150. strcpy(string," ");
  151. //transform(s.begin(),s.end(),s.begin(),toupper);
  152.  
  153. cout << "Please enter a line of text: " << endl;
  154.  
  155. cin.getline (string,100);
  156.  
  157. while(string[i]!=NULL)
  158. {
  159. S.push(string[i]); // push chars individually from string to
  160. Q.addq(string[i]); // stack and queue
  161. i++; // next char
  162. }
  163. while(i>0)
  164. {
  165. if(S.topNpop()==Q.frontNremoveq()) // compare each element from
  166. { // stack and queue
  167. RESULT=true; // if same for all chars return true
  168. }
  169. else
  170. {
  171. RESULT=false; // if not same for any char break and return false
  172. break;
  173. }
  174. i--;
  175. }
  176.  
  177.  
  178. if(RESULT==true)
  179. {
  180. cout<<string<<" \nThis is a palindrome\n"; // display if true
  181. }
  182. else
  183. {
  184. cout<<string<<" \nThis is not a palindrome\n"; // display if false
  185. }
  186.  
  187. cout<<s<<s<<endl;
  188.  
  189. return 0;
  190. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
mysterio is offline Offline
16 posts
since Sep 2005
Oct 12th, 2005
-1

Re: Help on stack , queue, palindrome program...

1. start out by replacing that C-style character array named "string" with a c++ string class and name it something else, such as line. You are writing a c++ program, so use c++ classes whenever possible. Not that it can't be done with C, just that it's more consistent.

2. replace cin.getline() with getline(cin,line)

3. paste that transform line after the getline() in #2 above


After making the above changes your program seems to work ok for the strings I entered.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 12th, 2005
0

Re: Help on stack , queue, palindrome program...

Quote originally posted by Ancient Dragon ...
1. start out by replacing that C-style character array named "string" with a c++ string class and name it something else, such as line. You are writing a c++ program, so use c++ classes whenever possible. Not that it can't be done with C, just that it's more consistent.

2. replace cin.getline() with getline(cin,line)

3. paste that transform line after the getline() in #2 above


After making the above changes your program seems to work ok for the strings I entered.
my program is C++ rite???
mind show me where should i add the transform function??
Reputation Points: 11
Solved Threads: 0
Newbie Poster
mysterio is offline Offline
16 posts
since Sep 2005
Oct 13th, 2005
0

Re: Help on stack , queue, palindrome program...

Quote originally posted by mysterio ...
my program is C++ rite???
Yes, it is.

Quote originally posted by mysterio ...
mind show me where should i add the transform function??
I already told you. You can do your own copy/paste, my fingers can't ready your keyboard :eek:
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 14th, 2005
0

Re: Help on stack , queue, palindrome program...

Quote originally posted by Ancient Dragon ...
Yes, it is.



I already told you. You can do your own copy/paste, my fingers can't ready your keyboard :eek:
show me at least example..then i can only can do it
how to make stack.h,queue.h,palindrome.cpp seperate files....??
Reputation Points: 11
Solved Threads: 0
Newbie Poster
mysterio is offline Offline
16 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Input with command line
Next Thread in C++ Forum Timeline: quick question- sprintf





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC