Convert to upper and back

So I have to create a program that take a string and brings it toupper then brings it back tolower however I cannot get it to do both.

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
// ConsoleApplication8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void convert(string& s)
{
	for (int i = 0; i < s.length(); i++)
	{
		s[i] = toupper(s[i]);
	}
}

void convert1(string& s)
{
	for (int i = 0; i < s.length(); i++)
	{
		s[i] = tolower(s[i]);
	}
}


int main()
{
	{
		string s;
		cout << "Enter a string" << endl;
		getline(cin, s);

		cout << "Here is the string in all upper case" << endl;
		convert(s);
		cout << s << endl;
	}
	{
		
		string s;
		getline(cin, s);
		cout << "Here is the string in all lower case" << endl;
		convert1(s);

		cout << s << endl;
	}

		return 0;
}


It works perfectly fine for me?

first Input: tarik
first output:
Here is the string in all upper case 
TARIK


second input: ITS ME
second output:
Here is the string in all lower case 
its me


Edit: cpp.sh/2iqf
Last edited on
He asked not clearly, but i think the lines 36 to 40 are his problem.
sorry, so I need to take the initial string that is entered, for example Hello World and it needs to be converted to "HELLO WORLD" first then "hello world" right after without inserting the string again
Then I suggest removing the "extra" braces, remove the second instance of the string s and the second getline().

Tried that it runs both functions but it does not show the results of both functions
Okay so that is fixed now, however now I have to run a reverse function where it takes a string and converts is to the opposite, for example "Hello World" would change to "hELLO wORLD"

This is the code so far.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void convert(string& s)
{
for (int i = 0; i < s.length(); i++)
{
s[i] = toupper(s[i]);
}
}

void convert1(string& s)
{
for (int i = 0; i < s.length(); i++)
{
s[i] = tolower(s[i]);
}
}

void convert2(string& s)
{
for (int i = 0; i<s.length(); i++) {
if ('a' <= s[i] && s[i] <= 'z') {
s[i] = char(((int)s[i]) - 32);
}

else if ('A' <= s[i] && s[i] <= 'Z') {
s[i] = char(((int)s[i]) + 32);
}
}


}


int main()
{
{
string s;
cout << "Enter a string" << endl;
getline(cin, s);

cout << "Here is the string in all upper case" << endl;
convert(s);
cout << s << endl;

cout << "Here is the string in all lower case" << endl;
convert1(s);
cout << s << endl;

cout << "Here is the string reversed" << endl;
convert2(s);
cout << s << endl;


}

return 0;
}

Keep getting this as a result

Enter a string
Hello World
Here is the string in all upper case
HELLO WORLD
Here is the string in all lower case
hello world
Here is the string reversed
HELLO WORLD
Press any key to continue . . .
I haven't really tried to see what you're doing wrong with your program but this is another way to do what you want.

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
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cctype>

void convert_to_upper(std::string& s)
{
    std::transform(s.begin(), s.end(), s.begin(), ::toupper);
}

void convert_to_lower(std::string& s)
{
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
}

void toggle_word_first_char(std::string& s)
{
    std::istringstream sin(s);
    std::ostringstream sout;
    std::string word;
    // Read each word from the string.
    while(sin >> word)
    {
        // Toggle the first character of the word.
        if(::isupper(word[0]))
            word[0] = ::tolower(word[0]);
        else
            word[0] = ::toupper(word[0]);
        // Write the modified word to the output stream.
        sout << word << ' ';
    }
    // Copy the modified output stream to the string.
    s = sout.str();
    // Remove the trailing space, if present.
    if(::isspace(s.back()))
        s.pop_back();
}


int main()
{
    std::string s = "Hello World";

    std::cout << "Here is the std::string in all upper case" << std::endl;
    convert_to_upper(s);
    std::cout << s << std::endl;

    std::cout << "Here is the std::string in all lower case" << std::endl;
    convert_to_lower(s);
    std::cout << s << std::endl;

    std::cout << "Here is the std::string reversed" << std::endl;
    toggle_word_first_char(s);
    std::cout << s << std::endl;

    toggle_word_first_char(s);
    std::cout << s << std::endl;

    return 0;
}
I don't want it to switch only the first character, which is what you effectively did....I need to have every character be the opposite of the casing
Well what have you tried? You should be able to take the toggle_word_first_char() and adapt it to toggle every character. The if() statement logic is the key.

When I even input your code is shows errors of the sin and sout, using different includes I see, just used your code the out put was exactly the same as the input.

Enter a string
Hello World
Here is the string in all upper case
HELLO WORLD
Here is the string in all lower case
hello world
Here is the string reversed
Hello World
Press any key to continue . . .
Last edited on
I know my code works because when I comment out the rest this is what returns

Enter a string
Hello World
Here is the string reversed
hELLO wORLD
Press any key to continue . . .

Which is right, what the program is doing is pulling the previous string then altering it, this is not what I need to happen, I need to to take the original string that was used from input and alter it.
http://www.cplusplus.com/reference/locale/toupper/

The C++ header <locale> defines an overloaded version of "toupper" that should make writing this program short and simple
I need to to take the original string that was used from input and alter it.

Then what is the problem? Can't you just use a different variable, or assign a new value to the current variable before you call that function?

No jib that is not what the program calls for

sMav that is great for changing a character toupper, which I have already done, now I need to do both changes to the same string.
Last edited on
Topic archived. No new replies allowed.