C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C++ String to array Posted by pistolizedpete on 10 Aug 2010 at 11:03 AM
Hi there,

New to C++ and trying to write a short program to reverse the characters in a word. Im having trouble converting the string to a char array. Here is the code:


#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
	string org;
	string end;
	cin >> org;

	int num = org.size();

	char reversal[num];
	strcpy(reversal, org.c_str());

	for (int a = num; a >= 0; a--)
	{
		cout << reversal[a];
	}
	cout << endl;
	return 0;
}




Im getting three errors. All are on the strcopy() line:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'reversal' : unknown size

Im using Visual C++ to compile.

Any advice you can give is much appreciated and if there is a more efficient way of doing this please let me know. Thanks again!

Pete.
Report
Re: C++ String to array Posted by semler9k on 10 Aug 2010 at 12:25 PM
for(std::string::reverse_iterator rit = org.rbegin(); rit !=  org.rend(); ++rit) {
  end += *rit;
}


This uses the reverse iterator of the string class to iterate backwards through the collection and appends each character to the new string.
Report
Re: C++ String to array Posted by semler9k on 10 Aug 2010 at 12:37 PM
BTW: Your compile problems is due to the fact that the compiler needs to know the size of the array when compiling. Otherwise you have to allocate the array dynamically using new (c++) or malloc (c)

char* reversal = new char [num];

Report
Re: C++ String to array Posted by pistolizedpete on 10 Aug 2010 at 2:00 PM
Thanks for all your help.

Pete.
Report
Re: C++ String to array Posted by HK_MP5KPDW on 12 Aug 2010 at 6:42 AM
char* reversal = new char [num+1];

Don't forget a C-style string (null terminated char array) needs that extra bit of space for the null terminating character. If it doesn't have a null it really isn't a "string" but rather simply a char array.

...and remember to always delete what you new.
Report
Re: C++ String to array Posted by bilderbikkel on 12 Aug 2010 at 6:26 AM
Report
Re: C++ String to array Posted by HK_MP5KPDW on 12 Aug 2010 at 6:35 AM
std::string ReverseString(const std::string& s)
{
  std::string t;
  std::copy(s.rbegin(),s.rend(),std::back_inserter(t));
  return t;
}

Can be rewritten simply as:
std::string ReverseString(const std::string& s)
{
  return std::string(s.rbegin(),s.rend());
}

...and, put that way, it almost doesn't even seem to deserve its own function.
Report
Re: C++ String to array Posted by bilderbikkel on 13 Aug 2010 at 1:34 AM
: ...and, put that way, it almost doesn't even seem to deserve its own
: function.
I purposefully post 'trivial' code on my website (http://richelbilderbeek.nl/CppWhyTrivialCode.htm): the beginner finds what he/she is looking for. The more advanced programmers use the one-line equivalent in his/her code directly.


Report
Thanks HK_MP5KPDW! Posted by bilderbikkel on 13 Aug 2010 at 1:39 AM
P.S. I put your shorter version on my site and thanked you for it: http://richelbilderbeek.nl/CppReverseString.htm

If you would not mind...

Thanks, Bilderbikkel
Report
Re: C++ String to array Posted by hero_hont on 19 Aug 2010 at 2:56 AM
i want know loop return... how does it work??
if we dom't use it, how do this code do?
[url=http://www.halongluxuryjunk.com]Halong bay cruises[/url]- [url=http://www.waytosapa.com]Sapa tours[/url]- [url=http://www.songxanhcruisemekong.com]Song Xanh Sampan Cruise Mekong[/url]
Report
Re: C++ String to array Posted by hero_hont on 19 Aug 2010 at 2:58 AM
i want know loop return... how does it work??
if we dom't use it, how do this code do?
[url=http://www.halongluxuryjunk.com]Halong bay cruises[/url]- [url=http://www.waytosapa.com]Sapa tours[/url]- [url=http://www.songxanhcruisemekong.com]Song Xanh Sampan Cruise Mekong[/url]



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.