My function isn't working properly.

This isn't displaying the string. I'm not sure if the function is even correct.

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
 #include <iostream>
#include <cstring>
#include <string>
using namespace std;

void reverse(char*);

int main()
{
	
	const int size = 100;
	char c[size];

	cout << "Please enter a string." << endl;
	cin.getline(c, size);

	void reverse(char* c);
	
	return 0;

}

void reverse(char* c)
{
	
	for (int i = 0; c[i] != 0; i++)
	{
		if (c[i] >= 'a' && c[i] <= 'z')
		{
			if (isupper(c[i]))
			{
				tolower(c[i]);
			}
			else if (islower(c[i]))
			{
				toupper(c[i]);
			}
		}
	}
	cout << "The new string is " << c << endl;
}
Last edited on
line 17: change with

reverse( c ) ;

please read to learn call function:
http://www.cplusplus.com/doc/tutorial/functions/

with correction compile but your function dont change your string
Last edited on
I got the function to work now. I'm just not sure how to change the string.
Last edited on
I'm just not sure how to change the string.


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
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

void reverse(char*);

int main()
{
        
        const int size = 100;
        char c[size];

        cout << "Please enter a string." << endl;
        cin.getline(c, size);

        reverse( c );                                 // <===== function call, not function prototype
        
        return 0;

}

void reverse(char* c)
{
        for (int i = 0; c[i] != '\0'; i++)             // <===== Null-terminated
        {
                //   <=== Your outer loop would have changed lower case only, so removed
                //
                        if (isupper(c[i]))
                        {
                                c[i] = tolower(c[i]);  // <====== You have to actually ASSIGN to c[i]
                        }                              //         to get an updated value
                        else if (islower(c[i]))
                        {
                                c[i] = toupper(c[i]);  // <====== Likewise, you have to assign to c[i]
                        }
                // <===== end of loop removed
        }
        cout << "The new string is " << c << endl;
}
Topic archived. No new replies allowed.