Issue flipping the case of letters

Hi! I am having trouble with the flipped function of my code. I want it to display the opposite of whatever was given by the user(in terms of upper and lowercase letters), but it just displays the letters in all uppercase.
I will post my code, then my assignment instructions incase you would like further details. I appreciate all the feedback, thanks:)

Code:
#include <iostream>
#include <string>
using namespace std;

// Function prototypes
void upper(char *);
void lower(char *);
void flip(char *);

int main()
{
char string[100];

cout << "Enter the string: ";
cin.getline (string, 100);

cout << "\nThe uppercase string: " << endl;
upper(string);

cout << "\nThe lowercase string: " << endl;
lower(string);

cout << "\nThe Flipped string: " << endl;
flip(string);

return 0;
}

void upper(char *str)
{
int i = 0;
while(str[i] != '\0')
{
str[i] = toupper(str[i]);
cout << str[i];
++i;
}
}

void lower(char *str)
{
int i = 0;
while(str[i] != '\0')
{
str[i] = tolower(str[i]);
cout << str[i];
++i;
}
}

void flip(char *str)
{

int i = 0;
while(str[i] != '\0')
{
if(isupper(str[i]))
{
str[i] = tolower(str[i]);
}
else
{
str[i] = toupper(str[i]);
}
cout << str[i];
++i;
}

}

Instructions:
Write a program with three functions: upper, lower, and flip. Each function should accept a C-string as an argument. The upper function should step through all the characters in the string, converting each to uppercase. The lower functions should step through all the characters in the string, converting each to lowercase. The flip steps through the string, testing each character to determine if it is upper or lowercase. If upper, it should convert to lower, if lower it should convert to upper.
Last edited on
Your code takes in a string that will be a mix of upper and lower.

Then you turn that string into all upper.

Then you turn it into all lower.

So what kind of string are you passing to the function flip? Is it a mixed string? No, you just turned it into all lower. So what will flip do? Turn it into all upper.
So if I wanted the function to do the flip based on the original string, would I just to put that function before the other two?
That would work. Better, give each function a copy of the string that they can work on. That way, it doesn't matter what order you call the functions in.

While I'm here, for the future when you have the choice, don't use arrays. Don't use C-strings. Use a proper C++ std::string. I really mean this.

Arrays are not for beginners. C-strings are only when you have no choice. Lots of teachers start with them, and I really don't know why. They think they're doing you a favour, teaching C instead of C++ :/
Last edited on
C-strings are only when you have no choice.

That appears to be the case here. The instructions specifically say that the functions should accept a C-string.

Ctuser, it's a good habit to separate the doing from the displaying. In this case, upper(), lower() and flip() should just modify the string. The main program should then print the modified string. This makes the program much more flexible. What if the next assignment is to write upper case strings to a file? You wouldn't be able to use your current upper() function without changing it.

Prefer for loops to express the loop. They make loops much easier to read because all the looping logic is in one place all the "do this each time through the loop" logic is somewhere else.

Search for "DMH" on the comments for other points.
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
#include <iostream>
#include <cctype>		// DMH toupper/tolower defined here.
using namespace std;

// Function prototypes
char * upper(char *);		// DMH return pointer to string for convenience
char * lower(char *);
char * flip(char *);

int
main()
{
    char string[100];

    cout << "Enter the string: ";
    cin.getline(string, 100);

    cout << "\nThe Flipped string: " << flip(string) << endl;
    cout << "\nThe uppercase string: " << upper(string) << endl;
    cout << "\nThe lowercase string: " << lower(string) << endl;

    return 0;
}

char *
upper(char *str)
{
    // DMH - prefer "for" loops
    for (unsigned i=0; str[i] != '\0'; ++i) {
	str[i] = toupper(str[i]);
    }
    return str;
}

char *
lower(char *str)
{
    for (unsigned i=0; str[i] != '\0'; ++i) {
	str[i] = tolower(str[i]);
    }
    return str;
}

char *
flip(char *str)
{
    for (unsigned i=0; str[i] != '\0'; ++i) {
	if (isupper(str[i])) {
	    str[i] = tolower(str[i]);
	} else {
	    str[i] = toupper(str[i]);
	}
    }
    return str;
}

You need to copy first, I think.

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

enum ACTION{ U, L, F };

void change( char *str, ACTION a )
{
   for ( ; *str; str++ )
   {
      switch ( a )
      {
         case U:
            *str = toupper( *str );
            break;
         case L:
            *str = tolower( *str );
            break;
         case F:
            *str = isupper( *str ) ? tolower( *str ) : toupper( *str );
            break;
      }
   }
}

int main()
{
   char str[101], temp[101];

   cout << "Enter a line: ";
   cin.getline( str, 100 );

   strcpy( temp, str );   change( temp, U );
   cout << "Upper: " << temp << '\n';

   strcpy( temp, str );   change( temp, L );
   cout << "Lower: " << temp << '\n';

   strcpy( temp, str );   change( temp, F );
   cout << "Flipped: " << temp << '\n';
}


Enter a line: UPPER & lower CaSe MaTtEr In C++.
Upper: UPPER & LOWER CASE MATTER IN C++.
Lower: upper & lower case matter in c++.
Flipped: upper & LOWER cAsE mAtTeR iN c++.

Topic archived. No new replies allowed.