Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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();
}
}

Reply With Quote
  #2 (permalink)  
Old April 24th, 2005, 02:59 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #3 (permalink)  
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.

Reply With Quote
  #4 (permalink)  
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.
Reply With Quote
  #5 (permalink)  
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!!

Reply With Quote
  #6 (permalink)  
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:01 AM.


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