working w/ backward functions question.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 586 Views - Last Post: 31 August 2011 - 11:15 AM Rate Topic: -----

#1 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

working w/ backward functions question.

Posted 30 August 2011 - 12:19 PM

Disclaimer: This is not my work. I was refereed to it by another but I do have a question about it. Is there a simpler way to manage this type of code:
//*********************************************
// functioin backward accepts a pointer to
// a C string and displays that string backward
// *********************************************

void backward(char *str)
{
	// Get the subscript of the last character in the string
	int last = strlen(str) - 1;

	// Work backward through the string displaying each
	// character from the last to the first.
	for (int index = last; index >= 0; index--)
		cout << str[index];

	cout << endl;
}


Or is this as simple as something like this can get? My main interest is in the
int last = strlen(str) - 1;

Is that the only way to get get a string to appear backwards?

Is This A Good Question/Topic? 0
  • +

Replies To: working w/ backward functions question.

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1703
  • View blog
  • Posts: 3,261
  • Joined: 30-May 10

Re: working w/ backward functions question.

Posted 30 August 2011 - 12:40 PM

The code you have is about as simple as it's going to get.

I mean, it's pretty obvious to any programmer what is going on - and that after all is a useful goal in it's own right.

I can think of shorter functions, but I wouldn't call them "simpler".
Was This Post Helpful? 0
  • +
  • -

#3 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: working w/ backward functions question.

Posted 30 August 2011 - 12:41 PM

In this code, he prints the string backwards, but has not modified the original string at all. He is iterating through the string backwards by accessing the last character of the string, then the before last and so on..

As you can see he wrote index-- at the end of the for loop.

But to iterate through it you need to know the length of the string; hence the use of strlen.

So really it is the simplest way to print a string backwards on the command line(cout), but only for that purpose. you could only simplify it this much more I think:

void backward(char* str)
{
          for(int index = strlen(str)-1;index >= 0;index--)
                    cout << str[index];

          cout << endl;

}



The problem with this code is what if you pass NULL to backward?
Was This Post Helpful? 1
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: working w/ backward functions question.

Posted 30 August 2011 - 12:44 PM

IT is certainly not the only way to get a string to appear backwards:
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
void backward(const char* s);


int main() {
    backward("How will this look backwards?");
    return 0;
}

void backward(const char* s) {
    string str(s);
    reverse(str.begin(), str.end());
    cout << str;
}

Was This Post Helpful? 1
  • +
  • -

#5 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: working w/ backward functions question.

Posted 30 August 2011 - 12:55 PM

In terms of 'managing' the original code, it is faulty because strlen is really a C function
Was This Post Helpful? 0
  • +
  • -

#6 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

Re: working w/ backward functions question.

Posted 30 August 2011 - 12:58 PM

View PostNickDMax, on 30 August 2011 - 12:44 PM, said:

IT is certainly not the only way to get a string to appear backwards:
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
void backward(const char* s);


int main() {
    backward("How will this look backwards?");
    return 0;
}

void backward(const char* s) {
    string str(s);
    reverse(str.begin(), str.end());
    cout << str;
}

I totally forgot about the member funtions Nick.
 reverse(str.begin(), str.end());

Im still an infant in this game.
Was This Post Helpful? 0
  • +
  • -

#7 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: working w/ backward functions question.

Posted 30 August 2011 - 12:59 PM

The first example would be better for a new programmer to grasp the concepts of arrays and how C++ would handle a C string.

The strlen function requires the <cstring> header file as well. After you get the basics, then you can move up to more advanced header files and using STL.
Was This Post Helpful? 0
  • +
  • -

#8 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

Re: working w/ backward functions question.

Posted 30 August 2011 - 12:59 PM

View PostDustyD, on 30 August 2011 - 12:41 PM, said:

In this code, he prints the string backwards, but has not modified the original string at all. He is iterating through the string backwards by accessing the last character of the string, then the before last and so on..

As you can see he wrote index-- at the end of the for loop.

But to iterate through it you need to know the length of the string; hence the use of strlen.

So really it is the simplest way to print a string backwards on the command line(cout), but only for that purpose. you could only simplify it this much more I think:

void backward(char* str)
{
          for(int index = strlen(str)-1;index >= 0;index--)
                    cout << str[index];

          cout << endl;

}



The problem with this code is what if you pass NULL to backward?

Thanks for the snippet DustyD
Was This Post Helpful? 0
  • +
  • -

#9 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

Re: working w/ backward functions question.

Posted 30 August 2011 - 01:05 PM

View PostJ-e-L-L-o, on 30 August 2011 - 12:59 PM, said:

The first example would be better for a new programmer to grasp the concepts of arrays and how C++ would handle a C string.

The strlen function requires the <cstring> header file as well. After you get the basics, then you can move up to more advanced header files and using STL.

Ill be glad when I figure out all the ins and outs of this stuff. Learning this stuff on your own is a bit daunting at times.
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: working w/ backward functions question.

Posted 30 August 2011 - 01:08 PM

@J-e-L-L-o - I am increasingly moving farther and farther away from this position that it is "better" to teach beginners the char-arrays version first.

First off it discourages beginners from thinking in C++ - instead they learn how to think very C-like and don't even really think to use the functions available to them in the language proper.

i.e. I really think that learning C++ from a C-with-classes point of view is really detrimental to learning C++ because it takes SO LONG to unlearn the C.
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: working w/ backward functions question.

Posted 30 August 2011 - 01:15 PM

@neato0z -- those are not "member functions" of a C-string but of a C++ std::string object.

What I did was change the problem space, rather than solving the problem in C, I converted it to a C++ problem by capturing the C-string in a C++ container std::string.

What this did do that your original did not was copy the string into some buffer (internal to the object str). So rather than using strlen() to find the length of the string the std::string class will capture the size of the string when it copies it over. -- this *may* actually be a performance hit but it greatly simplified the problem.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,752
  • Joined: 16-October 07

Re: working w/ backward functions question.

Posted 30 August 2011 - 01:15 PM

Yep, that's about it. You have to find how long the string is not matter what...

Well, sort of:
void backward(char *s) {
	if (*s!=0) {
		backward(s + 1);
		cout << *s;
	}
}


Was This Post Helpful? 1
  • +
  • -

#13 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: working w/ backward functions question.

Posted 30 August 2011 - 01:19 PM

But computers are not ONLY C++. It gives you insights into how the computer handles information. If you want to be just a developer, fine learn vector and string classes and be done with it. If you want to learn how the computer works, in different environments and languages, its so much easier to start with the base and move forward.

You don't have to be a mechanic to drive a car, you just get in and drive. But if something breaks, you would know what to look for and steps to take to fix it if you had a lil mechanical know how.

I guess I have a "know the basics" attitude to everything I do. I want to know "why". I am a computer science student though, not a computer programmer.
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: working w/ backward functions question.

Posted 30 August 2011 - 01:49 PM

So with that thinking would it not be better to learn something like:
void backwardC(const char* s) {
    const char*  begining = s;
    //look for the terminating 0x00 char
    while(*s != 0x00) { s++; }
    //note *s is 0x00 so we need to back up one place;
    while (s-- > begining) {
        cout << *s;
    };
}


This does more to teach you how things are working "under the hood" than the strlen method, no need to worry about learning the C function instead we just use our knowledge of what a char-array string is.
Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: working w/ backward functions question.

Posted 30 August 2011 - 06:08 PM

> It gives you insights into how the computer handles information

Of course, in the next breath, people will talk about how the language is portable.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2