C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 3rd, 2003, 02:22 PM
o0 IceFreeze 0o o0 IceFreeze 0o is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 o0 IceFreeze 0o User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Need help reversing a string

My problem is that when I run this program my output is not right, it should reverse the string, but instead it prints out random characters. Any help would be appreciated




PHP Code:
#include <iostream.h>

char string[11];

void reverse()
{
    
int character 9;
    
int character_two 0;
    
char string_two[11];
    while(
character != -1)
    {        
        
string[character] = string_two[character_two];
        
character--;
        
character_two++;
    }
    
cout << string << endl;
}
     
main()
{
    
char string[11];
    
cout << "Enter a string of characters to reverse, no longer than 10 characters" << endl;
    
cin.get(string11);
    
cin.ignore(50'\n');
    
reverse();
    return 
0;


Reply With Quote
  #2  
Old February 3rd, 2003, 02:35 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
Ok, since you allocated 11 bytes and print out a message asking for no more than 10 chars, you know that strings in C are NUL-terminated. So far, so good

But in reverse(), you assume that the user will enter exactly 10 chars. If I enter "hey", you'll be copying way past the end of the string, since you start at 9.

What you need to do, is look for the terminating NUL-byte, and then work your way back. Below is some code I slapped together for the occasion; don't look at it if you want to work it out yourself first.

Code:
void reverse (char *s)
{
        int i, j;
        char c;

        for (i = 0; s[i]; i++)
                ;       /* do nothing */

        i--; /* step back from the NUL-byte */

        for (j = 0; j < i; i--, j++) {
                c = s[j];
                s[j] = s[i];
                s[i] = c;
        }
}


I hope this makes sense to you.
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #3  
Old February 3rd, 2003, 04:42 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
Quote:
What you need to do, is look for the terminating NUL-byte, and then work your way back


Here's another example:

Code:
void reverse(char* s)
{
    char temp, *end;

    // point to last non-null char
    end = s + strlen(s) - 1;

    while (s < end) {
        temp   = *s;
        *s++   = *end;
        *end-- = temp;
    }
}


OR, in C++:

Code:
#include <string>
using namespace std;

int main(int argc, char** argv)
{
    string s = "raboof";
    string::reverse_iterator ri;
    string s2;

    for (ri = s.rbegin(); ri != s.rend(); ri++)
        s2 += *(ri.base()-1);

    cout << s2 << endl;
    return 0;
}

Reply With Quote
  #4  
Old February 3rd, 2003, 05:10 PM
o0 IceFreeze 0o o0 IceFreeze 0o is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 o0 IceFreeze 0o User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks to both of you

Reply With Quote
  #5  
Old February 4th, 2003, 08:32 AM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 19 h 1 m 34 sec
Reputation Power: 12
Another issue is that you input the string into one instance of "string" and then you reverse another instance that is uninitialized.

Reply With Quote
  #6  
Old February 4th, 2003, 10:45 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
Unless we are doing homework there is

_strrev()

from string.h
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
  #7  
Old February 5th, 2003, 12:55 AM
wild_pointer wild_pointer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 wild_pointer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If you're not doing homework, you might as well save yourself a headache and use std::string and std::reverse (found in <algorithm>).

Reply With Quote
  #8  
Old February 5th, 2003, 12:38 PM
o0 IceFreeze 0o o0 IceFreeze 0o is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 o0 IceFreeze 0o User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hm...looking back over my problem I fixed this quite easially...
I changed:

PHP Code:
 string[character] = string_two[character_two]; 


into:
PHP Code:
 string_two[character_two] = string[character]; 

Reply With Quote
  #9  
Old February 5th, 2003, 12:40 PM
o0 IceFreeze 0o o0 IceFreeze 0o is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 o0 IceFreeze 0o User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I also needed to change the while loop, and added

PHP Code:
|| string[character] != '\0'


edit: should be != '\0' , but it didn't show up.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help reversing a string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway