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
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);
}
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 . . .
#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;
}
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 . . .
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.