User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,550 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,574 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 754 | Replies: 6 | Solved
Reply
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Question another "segmentation fault"

  #1  
Jul 15th, 2006
plz look at this code and tell me where I'm going wrong. I can't use another array to reverse the string.
const int MAX_SIZE = 15; // Max word size of word, allow for '\0' 
 
void reverse(char word[]);     // function prototype 
 
int main() { 
    
  char word[MAX_SIZE]; 
 
  cout << endl << "Enter a word : "; 
  cin >> word; 
  cout << "You entered the word \"" << word << "\"" << endl; 
 
  reverse(word); 
 
  cout << "The word in reverse order is " << word << endl; 

    return 0; 
} // main 
 
void reverse(char word[]) { 
   // you can have local scalar variables of type int and char 
   // However, you cannot have any local array variables 
   // This function will reverse the characters in the array word 
 
    char ch;
    
    for(int i = 0, j = MAX_SIZE; i != j; i++, j--) {
            ch = word[j];
            if( (ch >= 'a' || ch >= 'A' ) && (ch <= 'z' || ch <= 'Z') )
            word[i] = ch;        
        
    } 
 
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: another "segmentation fault"

  #2  
Jul 15th, 2006
You don't want to start j one beyond the end of the array (very much like you said you fixed in your last thread), you want to start it at the end of the text. And it would be better to use <= instead of != (think of what may be missed if you have an even or odd number of characters). And you want to swap front-to-back.
http://www.daniweb.com/code/snippet259.html
Last edited by Dave Sinkula : Jul 15th, 2006 at 6:31 pm.
Reply With Quote  
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: another "segmentation fault"

  #3  
Jul 15th, 2006
i can't follow that code. we've just started with pointers, and i'm not supposed to use them on this example, but the next problem will require pointers.
Reply With Quote  
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Question Re: another "segmentation fault"

  #4  
Jul 15th, 2006
i'm getting closer. world comes back as worow. here's what i got..

   int a, b, i;    
    
    for(i = 0; word[i] != '\0'; i++) {
        a = i;    
    }
        b = a;
    for(i = 0; i <= b; i++, a--) {
        word[i] = word[a];
    }
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: another "segmentation fault"

  #5  
Jul 15th, 2006
This is not a swap:
word[i] = word[a];
And perhaps you mean i <= a as the condition:
for(i = 0; i <= b; i++, a--)
Reply With Quote  
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: another "segmentation fault"

  #6  
Jul 15th, 2006
i'll think about that.......... thx
Reply With Quote  
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: another "segmentation fault"

  #7  
Jul 15th, 2006
ch = word[a]
word[a] = word[i]
word[i] = ch

it works and i'm sure something like this only harder will be on the test. thx
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:15 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC