# Any Sample???



## melvynlyc (Aug 19, 2005)

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


----------



## johnwill (Sep 26, 2002)

I think the object of the homework is you're supposed to learn to program, not to copy/paste the code from us. :smile:

We'll be glad to help if you run into a problem, but what's the point of providing the program, what will you learn in that case?


----------



## melvynlyc (Aug 19, 2005)

okok...sorry.

Then how to i determine whether a word is in uppercase and how to changed it to lower case? Is there any function or what to make the compiler to change it itself?


----------



## elf (Jul 19, 2002)

I'd check it with the ascii code...

lower case letters number from 97 to 123 
upper case letters number from 65 to 90


```
if (*var > 96 && *var < 124);
printf("is lower case");
else if (*var > 64 && *var < 91);
printf("is upper case");
```
Mnd you that is C and you never specified what language you were programming in. Regardless that method should work for just about any language, although you'll have to change the syntax slightly. 

...oh and *var should be replaced with whatever variable you stored the line of text as.


----------



## johnwill (Sep 26, 2002)

You can use strlwr() or strupr() to convert strings to lower or upper case, then it's easy to compare them. You can also do case insensitive string comparisons strcmpi().


----------



## elf (Jul 19, 2002)

Yea I always forget about those functions...when I was in my C class we had to write a program for a test (in class) that converted the string to upper case...I did it the hard way with the ascii code. Surprisingly I was one of the few people who passed the test (and one of 3 who passed the class).


----------



## mgoldb2 (Dec 16, 2004)

This is an example of what you must do *WITH OUT USING STACKS AND QUEUE*. I do not have the time to make a Queue and Stack class and I dont seem to have one on my computer right now.


```
#include<iostream>
#include<istream>

using namespace std;

int main()
{
	char x[50];
	cout<<"Enter line of text"<<endl<<endl;
	cin.getline(x,256,'\n');
	char stack[50];
	char que[50];
	cout<<x;
	strlwr(x);
	int length = strlen(x);
	length--;
	int y=0;
	int z=length;
	int c=0;
	int a=0;

	while(z>=0)
	{
		
		if (x[c] > 96 && x[c] < 124)
		{
		stack[y]=x[c];
		y++;
		}
		if (x[z] > 96 && x[z] < 124)
		{
		que[a]=x[z];
		a++;
		}
		c++;
		z--;
	}
	stack[y]='\0';
	que[y]='\0';
	
	length=strlen(stack);
	length--;
	z=0;
	bool found=true;

	
	while(found==true && length>=0)
	{
		if(stack[z]!=que[z])
		{
			found=false;
		}
		z++;
		length--;
	}
		
	if(found==false)
	{
	cout<<" is not a palindrome"<<endl;
	}
	else
	{
	cout<<" is a palindrome"<<endl;
	}
	return 0;	
}
```
What you must do to make this meet your class requirments are the following. Implement a QUEUE class with the functions int getsize(), char DEQUEUE(), char ENQUEUE() and a STACK class with the functions int getsize(), char push(), char(pop) then you change the code


```
char stack[50];
	char que[50];
```
too


```
stack stack;
queue que
```
and


```
if (x[c] > 96 && x[c] < 124)
		{
		stack[y]=x[c];
		y++;
		}
		if (x[z] > 96 && x[z] < 124)
		{
		que[a]=x[z];
		a++;
		}
```
too


```
if (x[c] > 96 && x[c] < 124)
		{
stack.push(x[c]);
		}
		if (x[z] > 96 && x[z] < 124)
		{
que.enque(x[z]);
		}
		
	}
```
and


```
length=strlen(stack);
```
to


```
length=stack.getlength()
```
and


```
while(found==true && length>=0)
	{
		if(stack[z]!=que[z])
		{
			found=false;
		}
		z++;
		length--;
	}
```



too



```
while(found==true && length>=0)
	{
char test1=stack.pop()
char test2=que.deque()

		if(test1!=test2)
		{
			found=false;
		}
		z++;
		length--;
	}
```
That should all work. If you need help implementing the stack class or QUEUE class let me know. Also any questions on the program let me know.

I did not test the changes needed to make it work with stack and queue. My orginal program is tested and works perfect. You might have to do a little tweaking I missed to make it work with a stack and queue class.

Like always my program is given as a method to learn the skills not so you dont have to do it. I provide a program ONLY because if you like me you learn best by looking at examples of code.


----------



## melvynlyc (Aug 19, 2005)

I really do have problem implementing the stack and queue class. Is it something like this???

template<class Type>
class queueType:
{
public:
int getsize();
char DEQUEUE();
char ENQUEUE();
};

template<class Type>
class stackType
{
public:
int getsize();
char push();
char (pop);
};


Is it that i need to implement it like this? and Which part of the program should i implement it? Do i need to create another header file for stack and queue? And i have some questions too...juz hoping to learn more. 

1.what is #include<istream> means?
2. cin.getline(x,256,'\n'); What does the x and 256 means?
3. if (x[c] > 96 && x[c] < 124 What does 96 and 124 refers to here?


Thank u very much for your help buddy...i agree that looking on codes do help me more effectively...a million thanks to u. And btw,can u make the program to be able to verify whether the input is in uppercase and if it is...the program will change it to lower case? I totally have no idea in this...hope to see from u again soon...thanks.


----------



## mgoldb2 (Dec 16, 2004)

> what is #include<istream> means?


This is input stream. I believe it is require to use the getline function of cin. I not sure about this but I put it there in case it was needed for that.



> 2. cin.getline(x,256,'\n'); What does the x and 256 means?


x is the array of chars that I declare as char x[50] earlier. 256 is the number of char it will allow to be enter. Now that I think about it that should be 50 since that the max the array can hold. '/n' is when to stop user input the \n stands for when the user hits enter. The reason you use cin.getline instead of cin>>x is because cin>>x skips all blanks spaces while .getline does not.
to sumerize these are the conditions for cin.getline

cin.getline(char array, number of char to take in, when to exist user input)


> if (x[c] > 96 && x[c] < 124 What does 96 and 124 refers to here?


96 and 124 refers to the ACSI values of chars. This tests to make sure it a lowercase char. Since we converted to lowercase earlier we dont have to worry about uppercase.


```
strlwr(x);
```
this command in my code converted all uppercase letters in a char array into lowercase. If you do this you really dont need to check if it uppcase or lowercase because it will just make everything lowercase no matter what.



> Is it that i need to implement it like this? and Which part of the program should i implement it? Do i need to create another header file for stack and queue?


Unlees you require to use it I would not bother with template right now.

I would use a header file for each classe Then most of the time you use anouther .cpp file to implment the classes.


```
class queueType:
{
  public:
         queueType(); 
         int getsize();
         char dequeue();
         void enqueue(char);
  private:
  char x[256];
   int size;

};

class stackType
{
  public:
         stackType(); 
         int getsize();
         char pop();
         void push(char);
  private:
  char x[256];
   int size;

};
```
Is what I would use


----------



## melvynlyc (Aug 19, 2005)

so does this mean i do need to create another header file and include it to my .cpp file?kinda blur with it...


----------



## melvynlyc (Aug 19, 2005)

Well if we can use strlwr(x); to convert all input to lowercase...is there any way we can make the program to change the uppercase to lower case and the lower case to upper case? How can we implement 2 functions at the same time?

For example: I am AI
Then after execution it will become: i AM ai

It will change the lower case letters to uppercase and at the same time changes the uppercase letters to lower case...this is really confusing me....


----------



## melvynlyc (Aug 19, 2005)

i am really stucked and lost...is it that i need to create a win32 console application then create 2 header files for queue and stack each? Plz help...i got 38+ errors and i am really confuse.


----------



## mgoldb2 (Dec 16, 2004)

melvynlyc said:


> i am really stucked and lost...is it that i need to create a win32 console application then create 2 header files for queue and stack each? Plz help...i got 38+ errors and i am really confuse.


There a few way to do it the method taught in most computer science class is you would have 5 files for your program

.cpp containg main program include both .h files in it
.cpp containg stack implementation include stack .h file in it
.cpp contain QUEUE implementation include qeue.h file in it
.h containing stack declaration
.h containg queue declaration


----------



## johnwill (Sep 26, 2002)

melvynlyc said:


> i am really stucked and lost...is it that i need to create a win32 console application then create 2 header files for queue and stack each? Plz help...i got 38+ errors and i am really confuse.


I renew my suggestion that you write your own code, since copying others work will do little for your education.


----------

