A little challenge: "Reverse a string"

  • (7 Pages)
  • +
  • « First
  • 5
  • 6
  • 7

93 Replies - 50634 Views - Last Post: 08 November 2015 - 09:13 AM

#91 KeyKrusher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 08-June 15

Re: A little challenge: "Reverse a string"

Posted 13 June 2015 - 10:26 AM

I totally got this. C++ makes it rather easy
#include <iostream>
#include <string>

int main() {
   std::cout << "Enter a string!";
   string inputString;
   std::cin >> inputString;
   std::endl;
   std::cout << "You're reversed string is: " << std::reverse(inputString) << endl;
}


Ahh, good 'ole std::reverse.
Was This Post Helpful? 0
  • +
  • -

#92 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 771
  • View blog
  • Posts: 2,715
  • Joined: 31-December 10

Re: A little challenge: "Reverse a string"

Posted 17 October 2015 - 02:15 PM

View PostKeyKrusher, on 13 June 2015 - 01:26 PM, said:

I totally got this. C++ makes it rather easy
#include <iostream>
#include <string>

int main() {
   std::cout << "Enter a string!";
   string inputString;
   std::cin >> inputString;
   std::endl;
   std::cout << "You're reversed string is: " << std::reverse(inputString) << endl;
}


Ahh, good 'ole std::reverse.

The only std::reverse() function I know of takes two arguments, not one. It takes the beginning and last iterator to the string.
Was This Post Helpful? 0
  • +
  • -

#93 dex73r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-March 15

Re: A little challenge: "Reverse a string"

Posted 07 November 2015 - 04:31 AM

void reverse_str(std::string& strng,int howManyWords)
{
	using std::cout; using std::cin; using std::endl;
	for (int i = 0; i < howManyWords; i++)
	{
		if (i != 0)
			cin >> strng;
		int length = strng.length();

		for (int j = length - 1; j + 1 > i; j--)
		{
			cout << strng[j];
		}
		cout << endl;
	}
}


Was This Post Helpful? 0
  • +
  • -

#94 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 4577
  • View blog
  • Posts: 14,908
  • Joined: 05-May 12

Re: A little challenge: "Reverse a string"

Posted 08 November 2015 - 09:13 AM

How is that reversing a string when your line 7 prints out the string correctly, rather than reversed?
Was This Post Helpful? 0
  • +
  • -

  • (7 Pages)
  • +
  • « First
  • 5
  • 6
  • 7