Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

code check

Posted on 2004-09-22
5
284 Views
Last Modified: 2010-04-01
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stack>

using namespace std;

typedef char item_t;
#define CHUNK_SIZE 100

class Stack_t
{
    item_t*  items;
    int      top;

public:
    Stack_t() : items(NULL), top(0) {}
    void push(item_t item)
    {
        if (top%CHUNK_SIZE == 0)
        {
            item_t* nitems = new item_t[top + CHUNK_SIZE];
            if (items != NULL)
            {
                for (int i = 0; i < top; i++)
                    nitems[i] = items[i];
            }
            delete []items;
            items = nitems;
        }
        items[top++] = item;
    }
    bool pop(item_t& item)
    {
        if (top == 0)
        {
            cout << "Stack is empty";
            return false;
        }
        item  = items[--top];
        if (top%CHUNK_SIZE == 0)
        {  
            item_t* nitems = NULL;
            if (top > 0)
            {
                nitems = new item_t[top];
                for (int i = 0; i < top; i++)
                    nitems[i] = items[i];
            }
            delete []items;
            items = nitems;
        }
        return true;
    }                            
};


int main()
{
   const int SIZE = 100;

   Stack_t S;
   item_t input[SIZE]    = { 0 };
   item_t S_output[SIZE] = { 0 };
   int i,length;

   printf("Enter a words to reverse> ");
   for(i=0,input[i]=getchar();input[i]!='\n';i++,input[i]=getchar())
   {
      S.push(input[i]);
   }

   length=strlen(input)-1;   // we don't need final '/n'

   for(i=0;i<length;i++)
   {
      S.pop(S_output[i]);
 
      if(input[i]!=S_output[i])
      {
           printf("\nThe words are not palindrome\n");
           cin >> i;
           return 0;
      }

      else // if(Q_output[i]==S_output[i])
           cout << S_output[i];
     }
     printf(" is a palindrome\n");
     
     putchar('\n');
     cin >> i;
     return 0;
}

output:

[edeloss2@pegasus lab2]$ g++ main.cpp
main.cpp: In function `int main()':
main.cpp:67: implicit declaration of function `int printf(...)'
main.cpp:68: implicit declaration of function `int getchar(...)'
main.cpp:91: implicit declaration of function `int putchar(...)'



0
Comment
Question by:edelossantos
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12126840
You need to add

#include <stdio.h>

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 12126851
BTW, to elaborate - these functions are part of the CRT (C Runtime Library), not of the STL
0
 

Author Comment

by:edelossantos
ID: 12126899
please explain palindrome, the code stops that this is not a palindrome.  Del
0
 

Author Comment

by:edelossantos
ID: 12126907
[edeloss2@pegasus lab2]$ a.out
Enter a words to reverse> del

The words are not palindrome
0
 
LVL 86

Expert Comment

by:jkr
ID: 12126961
Instead of

  printf("Enter a words to reverse> ");
  for(i=0,input[i]=getchar();input[i]!='\n';i++,input[i]=getchar())
  {
     S.push(input[i]);
  }


  length=strlen(input)-1;   // we don't need final '/n'

use

  printf("Enter a words to reverse> ");
  gets(input);
  for(i=0;input[i]!=NULL;i++)
  {
     S.push(input[i]);
  }


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question