Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 23rd, 2005, 09:39 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default reverse problem

Modify the following code, write a method "Reverse(char[] s1, char[] s2)", ths string s1 and s2 must be the same size, the string s1 should become a reverse copy of string s2. Internal to reverse, use the CharStack to perform the reversal.

using System;

public class CharStack
{
private const int EMPTY = -1;
private int maxLen;
private char[] s;
private int top;
public void SetStack(int maxSize)
{
s = new char[maxSize];
maxLen = maxSize;
top = EMPTY;
}
public void Reset() { top = EMPTY; }
public void Push(char c) { s[++top] = c; }
public char Pop() { return s[top--]; }
public char TopOf() { return s[top]; }
public bool Empty() { return (top == EMPTY); }
public bool Full() { return (top == maxLen - 1); }
}
class CharStackTest
{
public static void Main()
{
CharStack s = new CharStack();
string str = "My name is Don Knuth!";
char[] reStr = new char[str.Length];
int i = 0;
s.SetStack(str.Length); // stack can accommodate str
Console.WriteLine(str);
while ((i < str.Length) && !s.Full())
s.Push(str[i++]);
while (!s.Empty()) // print the reverse

Console.Write(s.Pop());
Console.WriteLine();
}
}

 
Old April 24th, 2005, 02:59 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What's this? Are you sharing useful code, or do you want us to do your homework?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old April 24th, 2005, 06:44 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Same in this one, I couldn`t do the question, please help.

 
Old April 25th, 2005, 05:56 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

well,have a look at the codes,
Code:
        private void reverse(char[] s1,char[] s2)
        {
            if(s1.Length!=s2.Length)
                throw new Exception("the lenghts should be equal");
            CharStack stack=new CharStack();
            stack.SetStack(s1.Length);
            foreach(char ch in s1)
                stack.Push(ch);
            for(int i=0;i<s2.Length;i++)
                s2[i]=stack.Pop();
        }
        private void ToUse()
        {
            string name="My name is Don Knuth!";
            char[] s2=new char[name.Length];
            this.reverse(name.ToCharArray(),s2);
            foreach(char ch in s2)
                textBox1.Text+=ch.ToString();
        }


_____________
Mehdi.
software student.
 
Old April 25th, 2005, 07:52 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it, thank you very much!!

 
Old April 25th, 2005, 08:12 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

no problem,
it's better to change the CharStack class
Code:
    public class CharStack
    {
        private const int EMPTY = -1;
        private int maxLen;
        private char[] s;
        private int top;
        public CharStack(int maxSize)
        {
            s = new char[maxSize];
            maxLen = maxSize;
            top = EMPTY;
        }
        public void Reset() { top = EMPTY; }
        public void Push(char c)
        {
            if(!this.Full())
                s[++top] = c;
            else 
                throw new Exception("stack is full");
        }
        public char Pop() 
        {
            if(!this.Empty())
                return s[top--];
            else 
                throw new Exception("stack is empty");
        }
        public char TopOf() { return s[top]; }
        public bool Empty() { return (top == EMPTY); }
        public bool Full() { return (top == maxLen - 1); }
    }
    instead of SetStack you can have a constructor also when you want to pop or push something you should check the stack.

_____________
Mehdi.
software student.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse Feed senthilworld VB How-To 1 October 25th, 2007 03:07 PM
get node reverse loop JL11450 XSLT 3 June 1st, 2007 12:05 PM
scalar and reverse crmpicco Perl 2 February 16th, 2007 08:18 AM
Reverse Engineering abdul_owiusa General .NET 0 July 18th, 2006 10:22 AM
reverse engineering? druid95 Pro VB 6 1 May 5th, 2006 10:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.