Backward String

I need help really bad :(

Page 1 of 1

5 Replies - 960 Views - Last Post: 05 February 2010 - 06:14 PM Rate Topic: -----

#1 GoldenGoldsby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-February 10

Backward String

Posted 04 February 2010 - 01:18 PM

Can someone show me an example or a function that accepts a pointer to a C-String as an argument and displays its contents backward. I am so stuck. Any help is appreciated.
Is This A Good Question/Topic? 0
  • +

Replies To: Backward String

#2 Jay93  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 04-August 09

Re: Backward String

Posted 04 February 2010 - 01:19 PM

You need to show some code, but ill give you a hint. If your using the "string" #include file, stringname.length() in a for loop...
Was This Post Helpful? 0
  • +
  • -

#3 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Backward String

Posted 04 February 2010 - 01:19 PM

GoldenGoldsby, post the code that you have produced and we will gladly take a look at it.
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Backward String

Posted 04 February 2010 - 01:20 PM

Get the string.
Start iterating backwards, from strlen(your_string) - 1 to 0. (this is a for loop actually)
Print your_string[loop_variable].
That's all.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: Backward String

Posted 04 February 2010 - 01:22 PM

View PostGoldenGoldsby, on 04 February 2010 - 02:18 PM, said:

Any help is appreciated.

Did you check the C/C++ Tutorials & Snippets?

It's really hard to offer help when you say you are stuck, but you don't provide the code that you are writing for this project. If you are asking for someone to write the code for you, then you've come to the wrong place. If you are having trouble getting started, then you need to talk to your instructor. If you have an actual question about code you've written, or are getting an error that you don't understand, well again, it's impossible to help you with out seeing what you have done so far.
Was This Post Helpful? 0
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Backward String

Posted 05 February 2010 - 06:14 PM

When learning to code, simple examples did help me.
So, here you go... hopefully this won't bring about too much bashing

#include <iostream>
#include <string>

using namespace std;

int main()
{
const char * stringOne="This is a string that should NOT be mutable"; // don't modify this (don't assign a value)
string stringTwo="This is a mutable string with functions to do certain things like tell you the length";

cout << "The length of stringTwo is " << stringTwo.length() << endl; //length() gives the string length ( strlen(); )
// for the C++ string =D
cout << "The first element of stringTwo is ";
cout << stringTwo[0] << endl; // array indices start at zero
cout << "The last element of stringTwo is ";
cout << stringTwo[(stringTwo.length()-1)] << endl; // notice the subscript [] 
cout << "The first element of the immutable string, stringOne, is ";
cout << stringOne[0] << endl;
cout << "The last element of the immutable string, stringOne, is ";
cout << stringOne[(strlen(stringOne)-1)] << endl; // array indices start at zero, last element is length - 1
// the parenthesis were included to make the order of operations clear

return 0;
}


This post has been edited by taylorc8: 05 February 2010 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1