| | |
How do you reverse string elements
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
//My function is but the problem is that i dont have a proper working main function
void reverse(int a[], int n) { // function: Reverses the elements of a. // parameters: // a inout array of values. // n in number of values in a, a[0]..a[n-1] // returns: nothing for (int i=0; i<n/2; i++) { int temp = a[i]; a[i] = a[n-i-1]; a[n-i-1] = temp; } return;
void reverse(int a[], int n) { // function: Reverses the elements of a. // parameters: // a inout array of values. // n in number of values in a, a[0]..a[n-1] // returns: nothing for (int i=0; i<n/2; i++) { int temp = a[i]; a[i] = a[n-i-1]; a[n-i-1] = temp; } return;
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
/*My function is but the problem is that i dont have a proper working main function*/
c Syntax (Toggle Plain Text)
void reverse(int a[], int n) { for (int i=0; i<n/2; i++) { int temp = a[i]; a[i] = a[n-i-1]; a[n-i-1] = temp; } }
Last edited by WolfPack; Nov 1st, 2006 at 6:05 am. Reason: Added [CODE][/CODE] tags
•
•
•
•
the problem is that i dont have a proper working main function
gr Niek
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
Here's a working example of how to reverse a string using a function. Hope this helps, good luck!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void ReverseString(char string1[], char reversed[]); int main() { const int MAX = 80; char string1[MAX]; char reversed[MAX]; cout << "Enter your string: "; cin.get(string1,MAX); ReverseString(string1, reversed); return 0; } void ReverseString(char string1[], char reversed[]) { int length = strlen(string1); for (int i=length-1, j=0; i>=0; i--, j++) reversed[j] = string1[i]; for (i=0; i<length; i++) cout << reversed[i]; cout << endl; }
•
•
•
•
Here's a working example of how to reverse a string using a function. Hope this helps, good luck!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void ReverseString(char string1[], char reversed[]); int main() { const int MAX = 80; char string1[MAX]; char reversed[MAX]; cout << "Enter your string: "; cin.get(string1,MAX); ReverseString(string1, reversed); return 0; } void ReverseString(char string1[], char reversed[]) { int length = strlen(string1); for (int i=length-1, j=0; i>=0; i--, j++) reversed[j] = string1[i]; for (i=0; i<length; i++) cout << reversed[i]; cout << endl; }
C++ Syntax (Toggle Plain Text)
void ReverseString(char string1[], char reversed[]) { int i, length = strlen(string1); for (int i=length-1, j=0; i>=0; i--, j++) reversed[j] = string1[i]; for (i=0; i<length; i++) cout << reversed[i]; cout << endl; cin.get(); cin.get(); }
Here's the reviewed code from may4life.
gr Niek
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
Hmm, dont know whats going wrong.. I copied and pasted my code from here and still works no prob on my compiler :-S
I always test my code and post it with 0 errors and 0 warnings.. Maybe there might be a mix up with our compilers.. Sorry for anyone who had problems with my code..but they work for me :-S
I always test my code and post it with 0 errors and 0 warnings.. Maybe there might be a mix up with our compilers.. Sorry for anyone who had problems with my code..but they work for me :-S
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Need help creating a simple Counter program using classes
- Next Thread: Simple Addition Question
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class code coding compile console conversion count data database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






