Reverse a string

Hello, this is my first time posting to these forums but they have been extremely helpful throughout my college semester. I have a program here that I have been working on for a week now and the criteria to it is very specific.

I have written and been able to get it to work but without following the criteria.
I could turn it in as is but I would really like to know this concept.

What I have to do is write a function that takes only 1 parameter and print it using pointers. The part I am having difficulty doing is keeping the cout out of the function itself.

What i have so far is like-so:

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

#include <iostream>
#include<cstring>
#include<string>
using namespace std;


void stringReverse(char *CString);

int main() 

{system("title  _                                                           Reverse String                                _");
 system("color 0c");

	
	char str[100];
	
	
		cout << "you can input a string of characters or a sentence and I will reverse it for you" << endl << endl;
		cin.getline(str, 99);
		cout << endl;
		cout << " your sentence or string in reverse is: " << endl << endl << endl;
		

		stringReverse(str); // string reversed function call
		cout << str << endl; // srting reprinted as is (forward)
		cout << endl;

system ("PAUSE");
	return 0;
}

void stringReverse(char *CString)  //changed this in class
{
	int len = strlen(CString);

	char *p = &CString[ len - 1];
		for(int i = 0 ;i < len; i++) 
			
			*p--;         //   minus, minus , instead of plus , plus, for reverse (decrement) (-- ! ++)
}





ever since i took the cout out of the function it will not reverse the string anymore. Can anyone help?
closed account (j3A5Djzh)
Hi, string reverse is easy :-)

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
#include <iostream>
#include<cstring>

using namespace std;

void stringReverse(char *);

int main(void) 
{
    char str[100];
    cout << "you can input a string of characters or a sentence and I will reverse it for you" << endl;
    cin.getline(str, 99);
    cout << endl;
    cout << "your sentence or string in reverse is: " << endl;
    stringReverse(str); // string reversed function call
    cout << str << endl; // srting reprinted as is (forward)
    return 0;
}

void stringReverse(char *cstr)
{
	int len = strlen(cstr);
    char swap;

    for (int i = 0; i < len/2; i++)
    {
        swap = cstr[i];
        cstr[i] = cstr[len-i-1];
        cstr[len-i-1] = swap;
    }
}
Hello, I thank-you for the reply!!

This is exactly what needs to happen with the program but I have to specifically use a single parameter within the function called CString. I tried switching cstr out from your example but it would not run since CString does not seem to be initializing.

the idea is to learn how to use the '\0' null character that comes with the CString array. That is the part that I do not understand. . .
Last edited on
Topic archived. No new replies allowed.