The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 03-28-2008, 02:07 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Reverse String C++

Why doesn't the code below works..? It throws some weird error from VS..

Code:
#include <iostream>
#include <string>

using namespace std;

void ReverseString (char *String)
{
	char *Begin = String;
	char *End = String + strlen(String) - 1;
	char TempChar = '\0';

	while (Begin < End)
	{
		Begin++;
		End--;
	}
}

int main()
{
	ReverseString("hello");
	return 0;
}
any ideas..?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 03-28-2008, 02:18 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 305
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Maybe if you tell us the errors we can help you more (:

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 03-28-2008, 02:33 PM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 317
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Icon5

Seems like their is some circular logic going on here... incrementing while decrementing - or is String a reserved word? or are you supposed to reference String as a pointer in the FUnction too? (like *String instead of String)?

Here's a recursive fuinction reversiing a string in C++

Code:
Recursive Reverse String Algorithm

The Shortest function to reverse a string.

Written by: Sanchit Karve
            born2c0de AT hotmail.com
[born2c0de]
*/

#include <iostream>

using namespace std;

void ret_str(char* s)
{
    if(*s != '\0')
         ret_str(s+1);

    cout<<*(s);
}

int main()
{
   ret_str("born2c0de");
   return 0;
}
Reply With Quote
The Following User Says Thank You to ccoonen For This Useful Post:
HelloWorld (03-28-2008)
  #4 (permalink)  
Old 03-28-2008, 05:21 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
much nicer code, but I hate recursive though lol... oh well, looks much nicer

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 03-29-2008, 06:06 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 305
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
You could use a for loop of the string's length and do something like

Code:
ReversedString[i] = String[StringLength-i];

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
HelloWorld (03-30-2008)
  #6 (permalink)  
Old 03-30-2008, 04:25 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
the problem is that "hello" is a string literal, and cannot be modified, so its undefined behaviour and VS is right to throw up an error.

change your main to something like this
Code:
int main()
{
    //"hello" is now copied into a char[]
    char str[] = "hello";
    ReverseString( str ); 
}
This is one of the reasons why char* is often considered bad, because there's no protection for programmers accidentally doing this sort of thing. in C++, if you always use the string type, you can't go wrong.

edit you're also missing the code which actually swaps the characters in your string - all you're doing is iterating 2 pointers which don't actually modify the char array

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Lee : 03-30-2008 at 05:42 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (03-30-2008)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 07:22 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50