|
-
February 16th, 2004, 08:15 PM
#1
Reverse fucntion
hi everyone
please tell me where did i go wrong,Thanks first.The question:
Write the function:
Code:
Void reverse(char s[], char s1[]);
The string s and s1 must be the same size.String s1 should be come a reversed copy of string s.Ex:
in put : My name is Jessica
out put: caissej si eman yM
Here what i have done so far:
PHP Code:
#include <iostream>
#include <cstdlib>
class reverse{
public:
void reset() {top = EMPTY;}
void push(char c) { top++; s[top] =c;}
char pop() {return s[top--];}
char top_of() const { return s[top];}
bool empty() const {return (top ==EMPTY);}
bool full() const {return (top ==FULL);}
private:
enum {max_len =100, EMPTY =-1,FULL = max_len-1};
char s[max_len];
int top;
};
int main()
{
reverse s;
int i;
char s1;
cout << "enter a sentence",
cin >> s1;
s.reset();
while (s1[i] && !s.full())
s.push(s1[i++]);
while (!s.empty())
cout << s.pop();
cout << endl;
system("PAUSE");
return 0;
}
-
February 16th, 2004, 08:38 PM
#2
Ain't you suppose to write a function, void reverse(char s1[], char s[]), rather than a class?
-
February 16th, 2004, 09:18 PM
#3
so you saying that i need to delete class?and write function reverse?
-
February 16th, 2004, 09:31 PM
#4
Re: Reverse fucntion
Originally posted by tinhnho
Write the function:
Code:
Void reverse(char s[], char s1[]);
The string s and s1 must be the same size.String s1 should be come a reversed copy of string s.Ex:
in put : My name is Jessica
out put: caissej si eman yM
Isn't the above stated that you need to write the function? IMHO, it isn't necessary to create a class just for the above transformation.
-
February 16th, 2004, 09:40 PM
#5
hi there
the question require me have to use this class:
Code:
class reverse{
public:
void reset() {top = EMPTY;}
void push(char c) { top++; s[top] =c;}
char pop() {return s[top--];}
char top_of() const { return s[top];}
bool empty() const {return (top ==EMPTY);}
bool full() const {return (top ==FULL);}
private:
enum {max_len =100, EMPTY =-1,FULL = max_len-1};
char s[max_len];
int top;
};
-
February 16th, 2004, 09:54 PM
#6
Why on earth are you using C-Style strings in C++? I would use std::string and I believe there is an algorithm for reversing a string (not sure, but I thought there was one in the algorithm library).
-
February 16th, 2004, 10:46 PM
#7
Code:
#include <string>
#include <algorithm>
using namespace std;
string a0("0123456789");
string a1;
a1.resize(a0.size()+1, '\0');
reverse_copy(a0.begin(), a0.end(), a1.begin());
-
February 17th, 2004, 12:14 AM
#8
Code:
.............
int main()
{
Reverse s;
int i=0;
char s1[100];
cout << "enter a sentence: ",
cin.getline(s1, 100);
s.reset();
while (s1[i] && !s.full())
s.push(s1[i++]);
while (!s.empty())
cout << s.pop();
cout << endl;
system("PAUSE");
return 0;
}
it works fine now,thanks everyone
-
February 17th, 2004, 12:15 AM
#9
tinhnho,
if you still want to use that class, you need to change your cin>>s1 into cin.getline(s1,100) or something similar that uses getline because cin will ignore white space chars
-
February 17th, 2004, 12:22 AM
#10
tinhnho, if you did as what I told you, you muct be sure you enter 100 chars, or it will bring all weird chars from behind...
You declared your max_len as 100 chars so, it will print from 99 to 0
-
February 17th, 2004, 06:00 AM
#11
Originally posted by tinhnho
it works fine now,thanks everyone
You didn't write a function that reverses the string (which was your original question). All you did was code something within the main() function that supposedly reverses the string.
What was the purpose of the Reverse class? It doesn't do what it says. If you call a class "Reverse", you would expect that it would do the "reversing", but it doesn't. It is a stack implementation.
I'm pointing all of these things out, since you may believe that it's working -- yes it may "functionally" work, but it doesn't meet the requirements of the assignment given.
Regards,
Paul McKenzie
-
February 17th, 2004, 12:11 PM
#12
thanks Paul McKenzie , i got what you were saying.i am working on it now.one again thank for telling me that.
-
February 17th, 2004, 01:01 PM
#13
tinhnho,
Here is what I did
Code:
void Reverse(char s1[],char s2[]){
for(int i=0,j=strlen(s1)-1; i<strlen(s1),j>=0; i++,j--)
s2[i]=s1[j];}
-
February 19th, 2004, 02:25 PM
#14
I suppose the professor was teaching the use of a stack to reverse things, but I don't know how much "deep understanding" will be gained by it. Plus it is an extremely klunky looking implementation.
Charleston's solution is basically a stack-based reversal, but cutting out the middleman and using the source string itself as the stack. If one wanted to be truly pedantic, one would read in reverse out of the source string and fill the destination string in order, 0, 1, 2...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|