Solved

code check

Posted on 2004-09-22
5
259 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now