case manipulator

I'm getting confused with the changing of the letters. I'm supposed to use 3 functions upper, lower, and reverse. The upper should accept a c-string as an argument and go through all the characters in the string, converting each to uppercase. the lower should accept a pointer to a c-string as an argument and go through all the characters in the string converting each to lover case. The reverse should also accept a c-string and test each character to determine whether it is upper or lower and change it

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
     
    char* reverse (char*, int);
    char* lower (char*, int);
    char* upper (char*, int);
     
    int main ()
    {
    const int SIZE = 80;
    char myCharString [SIZE];
    int strLength;
     
    //Prompt user for string
    cout << "Please enter a string.\n";
    //read in string
    cin.getline(myCharString, SIZE);
    //find and display length
    strLength = strlen(myCharString);
    cout << "Your string is " << strLength << " characters long\n";
     
     
    //call reverse function and output results in main to screen
    reverse(myCharString, strLength);
    {
    cout << "Your string reversed: ";
    for (int count = 0; count < strLength; count++)
    {
    cout << reverse(myCharString, strLength);
    cout << myCharString[count];
    cout << endl;
    }
    }
     
    //call lower function and output results in main to screen
    lower(myCharString, strLength);
    {
    cout << "Your string in lowercase: ";
    for (int count = 0; count < strLength; count++)
    {
     
    cout << lower(myCharString, strLength);
    cout << myCharString[count] ;
    cout << endl;
    }
    }
     
    //call upper function and output results in main to screen
    upper(myCharString, strLength);
    {
    cout << "Your string in uppercase: ";
    for (int count = 0; count < strLength; count++)
    {
    cout << myCharString[count];
    cout << endl;
    }
    }
    }
    // function that reverses the case of string
        char* reverse (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    {
    if (isupper(string[count]))
    tolower(string[count]);
    else if (islower(string[count]))
    toupper(string[count]);
     
    }
    return(string);
    }
     
    // function that puts entire string in lower case
    char* lower (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    {
    tolower(string[count]);
    }return string;
    }
     
     
     
    // function that puts entire string in upper case
    char* upper (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    {
    toupper(string[count]);
    }return string;
    }
you could use strlen to work with only the string itself rather than step through the entire 50 byte size of the array.

Anyway, what is the problem? I don't see a question.
I think "Invert" would be less confusing that "Reverse" but that's just me. When you read into a cstring allocated as string[80] you can only read in 79 characters because there needs to be a place for the null terminator. If you never enter more than 79 everything is peachy but if you did then you'd go outside the bounds of the array.
1
2
const int SIZE = 80;
char str[SIZE+1]; // this can fit 80 characters *and* the null terminator 


Also tolower() and touppper() return values so you'd need to assign them
1
2
3
  string[count] = tolower(string[count]);
  string[count] = toupper(string[count]);
...


Last edited on
The question is

Write a program with three functions: upper,lower,and reverse. The upper function should accept a c-string as an argument. It should step through all the characters in the string, converting each to uppercase. The lower function, too, should accept a pointer to a c-string as an argument. It should step through all the characters in the string, converting each to lowercase. Like upper and lower, reverse should also accept a c-string. As it steps through the string, it should test each character to determine whether it is upper- or lowercase. If a character is uppercase, it should be converted to lowercase. If a character is lowercase, it should be converted to uppercase. Test the functions by asking for a string in function main, then passing it to them in the following order: reverse, lower, and upper
That's not a question.
How is it not a question, i'm supposed to write a program that does 3 functions. My question is how do i fix the program to change the text from upper case to lower case and from lower case to upper case. And I also need to do a reverse function
It's not a question, because it doesn't fit the definition of a question. Perhaps you can tell me how "Write a program" is a question?


My question is how do i fix the program to change the text from upper case to lower case and from lower case to upper case.


What do you expect to happen with the code you have, and how does what you expect to happen differ from what actually happens?

"My homework doesn't work. Fix it." isn't going to get you many constructive responses.
I'm getting confused with the toupper and tolower. I dont need to put a loop to display the strings right?
okay, i was doing some work to it and i think i got the upper and lower to work. I just can't figure out the reverse. how would i go about working on it

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
     
    char* reverse (char*, int);
    char* lower (char*, int);
    char* upper (char*, int);
     
    int main ()
    {
    const int SIZE = 80;
    char myCharString [SIZE];
    int strLength;
     
    //Prompt user for string
    cout << "Please enter a string.\n";
    //read in string
    cin.getline(myCharString, SIZE);
    //find and display length
    strLength = strlen(myCharString);
    cout << "Your string is " << strLength << " characters long\n";
     
     
    //call reverse function and output results in main to screen
    reverse(myCharString, strLength);
    {
    cout << "Your string reversed: ";
    cout << reverse(myCharString, strLength);
	cout<<endl;
    }
     
    //call lower function and output results in main to screen
    lower(myCharString, strLength);
    {
    cout << "Your string in lowercase: ";
    cout << lower(myCharString, strLength);
    }
	cout<<endl;
    
     
    //call upper function and output results in main to screen
    upper(myCharString, strLength);
    {
    cout << "Your string in uppercase: ";
    cout<<upper(myCharString, strLength);
	cout<<endl;
    }
    }
    // function that reverses the case of string
        char* reverse (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    {
    if (isupper(string[count]))
    tolower(string[count]);
    else if (islower(string[count]))
    toupper(string[count]);
     
    }
    return(string);
    }
     
    // function that puts entire string in lower case
    char* lower (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    {
    string[count]=tolower(string[count]);
	}
    return string;
	} 
     
     
    // function that puts entire string in upper case
    char* upper (char * string, int SIZE)
	{
    for(int count = 0; count < SIZE; count++)
    { 
    string[count]=toupper(string[count]);
	}
    return string;
    }
You can likely figure this out by looking at your other functions:

1
2
3
4
5
6
7
8
9
10
11
    // function that reverses the case of string
        char* reverse (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    {
    if (isupper(string[count]))
      string[count] = tolower(string[count]);
    else
      if (islower(string[count]))
        string[count] = toupper(string[count]);
    }
thanks, but it still didn't work.
Topic archived. No new replies allowed.