•
•
•
•
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
![]() |
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;
}
} 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
http://www.daniweb.com/code/snippet259.html
Last edited by Dave Sinkula : Jul 15th, 2006 at 6:31 pm.
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];
} This is not a swap: And perhaps you mean i <= a as the condition:
word[i] = word[a];
for(i = 0; i <= b; i++, a--)![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- segmentation fault (C)
- Access Violation (Segmentation Fault) + atol (C++)
- unix/C++ segmentation fault (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: How to read data from csv file in an array and parse
- Next Thread: Trouble shooting help



Linear Mode