With introductory homeworks you will not likely have problems with plagarism. But I am glad to see you are interested in learning more than copying.
Each of this is a simple modification of an array of characters. For example, suppose I give you:
std::string s = "Hello world!";
Can you write a function that makes that all uppercase?
1 2 3 4 5 6 7 8
std::string uppercase( const std::string& s )
{
std::string result = s;
// what can I do to result here to make all its characters uppercase? //
return result;
}
Hi am new to this.
Just wanted someone to help me with my assignment to asks me to do:
Make a menu
1:Enter a word.
2:Reverse word than been enetered.
3:Randomise the word.
4:Upper case the word
5:Lower case it
6:Organise in the alapabetical order of the word.
Exit.
It has to have a menu where the user renter a word then presses a number for example 2 then the program
reverses the word etc.
Thanks looking forward for the replies.
And yea I have started it and got basic codes done but am struggling on writing codes that aren’t from
online(plagiarism reasons)
...I have started it and got basic codes done...
Could you specify what type of help do you need? It would be easier to pin point based on your code.
Do you know how to use the switch case for the menu?
Can you use the string library?
Without seeing your code is a little hard to pin point what help do you need.
#include <iostream>
usingnamespace std;
int main()
{
int C;
string word;
cout<<"Enter a word: ";
cin>>word;
cout<<endl<<"1:Reverse word than been enetered.";
cout<<endl<<"2:Randomise the word.";
cout<<endl<<"3:Upper case the word";
cout<<endl<<"4:Lower case it";
cout<<endl<<"6:Organise in the alapabetical order of the word.";
cout<<endl<<"Other numbers to EXIT" //OPTIONAL
//If you click other numbers you exit
cout<<endl<<"Choose: ";
cin>>C;
if(C=1)
{
//........
}
elseif(C=2)
{
//........
}
elseif(C=3)
{
//........
}
elseif(C=4)
{
//........
}
elseif(C=5)
{
//........
}
elseif(C=6)
{
//........
}
/* OR YOU CAN USE THE "switch" FUNCTION
switch(C)
{
case 1:
//........
break;
case 2:
//........
break;
case 3:
//........
break;
case 4:
//........
break;
case 5:
//........
break;
case 6:
//........
break;
}
*/
return 0;
}
It's like cold calling. I don't know you from Adam and you just ask for help with an assignment. People here give their time freely and don't want to be duplicating effort that others might have spent answering you in private.
If you are worried about people on the same course copying your code, write something that shows the issue you are having but not the details of your solution...doing this might even lead to you solving your own issues.
#include <iostream>
#include <string>
#include <random>
std::string reverse(const std::string);
int randomNumber(int, int);
std::string randomizeWord(const std::string);
int main()
{
std::string originalWord{"luminescent"};
std::cout << "Original Word: " << originalWord << std::endl;
std::cout << "Reverse Word: " << reverse(originalWord) << std::endl;
std::cout << "Randomized Word: " << randomizeWord(originalWord) << std::endl;
}
/*
This function takes the original word from the user
and reverse it by using the for loop with the counter
set to the size of the word from the user and will save
each single character into the result variable.
*/
std::string reverse(const std::string originalWord)
{
std::string result{ originalWord };
std::size_t increment{0}; // Counter to be used as increment for each char it takes.
for (std::size_t count = originalWord.size(); count > 0; count--)
{
result[increment] = originalWord[count - 1];
increment++;
}
return result;
}
std::string randomizeWord(const std::string originalWord)
{
std::string tempWord{ originalWord }; // Save the user inputted string into a temp to be manipulated.
std::string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
std::string result{}; // To save the randomize word and return it back to the function caller.
while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
{
constint randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
result.append(singleCharacter); // Append the character into the result string.
tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
}
return result;
}
/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> distrib(min, max);
constint random = distrib(rng);
return random;
}
#include <iostream>
#include <string>
#include <random>
#include <limits> // To be used with the cin.ignore.
//Function Prototypes.
std::string reverse(const std::string);
int randomNumber(int, int);
std::string randomizeWord(const std::string);
void displayMenu(int&);
std::string getWord();
int main()
{
std::string originalWord{};
bool keepLooping{true};
int choice{};
while (keepLooping)
{
displayMenu(choice);
switch (choice)
{
case 1:
std::cin.ignore(std::numeric_limits < std::streamsize >::max(), '\n');
originalWord = getWord();
std::cout << "Original Word: " << originalWord << std::endl;
keepLooping = true;
break;
case 2:
std::cout << "Reverse Word: " << reverse(originalWord) << std::endl;
keepLooping = true;
break;
case 3:
std::cout << "Randomized Word: " << randomizeWord(originalWord) << std::endl;
keepLooping = true;
break;
case 4:
// Call the function to upper case the word here.
keepLooping = true;
break;
case 5:
// Call the function to lower case the word here.
keepLooping = true;
break;
case 6:
// Call the function to alphabetize the word here.
keepLooping = true;
break;
case 7:
std::cout << "Closing the program...\n";
keepLooping = false;
break;
default:
std::cout << "Please choose between 1-7\n";
keepLooping = true;
}
}
}
/*
This function takes the original word from the user
and reverse it by using the for loop with the counter
set to the size of the word from the user and will save
each single character into the result variable.
*/
std::string reverse(const std::string originalWord)
{
std::string result{ originalWord };
std::size_t increment{0}; // Counter to be used as increment for each char it takes.
for (std::size_t count = originalWord.size(); count > 0; count--)
{
result[increment] = originalWord[count - 1];
increment++;
}
return result;
}
std::string randomizeWord(const std::string originalWord)
{
std::string tempWord{ originalWord }; // Save the user inputted string into a temp to be manipulated.
std::string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
std::string result{}; // To save the randomize word and return it back to the function caller.
while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
{
constint randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
result.append(singleCharacter); // Append the character into the result string.
tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
}
return result;
}
void displayMenu(int &choice)
{
std::cout << "\n String Manipulator\n";
std::cout << "-----------------------------------------\n";
std::cout << "1) Enter the word\n";
std::cout << "2) Reverse the word\n";
std::cout << "3) Randomize the word\n";
std::cout << "4) Upper case the word\n";
std::cout << "5) Lower case the word.\n";
std::cout << "6) Alphabetical order the word.\n";
std::cout << "7) Exit the program\n\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
}
/*
This function is to obtain the word from the user.
It uses the getline to obtain multiple words with
spaces (i.e. Programming is fun!). It will return
the word back to function caller.
*/
std::string getWord()
{
std::string word{};
std::cout << "Enter a word: ";
std::getline(std::cin, word);
return word;
}
/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> distrib(min, max);
constint random = distrib(rng);
return random;
}
This is the code i have for lowercase and uppecase:
First, I recommend that you break it down into two functions. Actually, you have broke it down here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Convert the string to upper case and return the new string
string
stringToUpper(string currentString)
{
cout << "StringToUpper is not yet implemented\n";
return currentString;
}
// Convert the string to lower case and return the new string
string
stringToLower(string currentString)
{
cout << "StringToLower is not yet implemented\n";
return currentString;
}
An example of how to lowercase the word would be like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
This function takes the word inputted by the user
and uses a for loop to iterate each character
and lower the case to be saved into a result variable
to be return to the caller function.
*/
std::string lowerCase(const std::string originalWord)
{
std::string result{originalWord};
for (std::size_t count = 0; count < result.size(); count++)
{
result[count] = std::tolower(originalWord[count]);
}
return result;
}
Another suggestion is to avoid the goto. You should use functions, instead.
#include <iostream>
#include <string>
#include <random>
#include <cctype> // To be used for tolower and toupper.
#include <limits> // To be used with the cin.ignore.
//Function Prototypes.
std::string reverse(const std::string);
std::string randomizeWord(const std::string);
std::string lowerCase(const std::string);
std::string upperCase(const std::string);
std::string alphabetize(const std::string);
int randomNumber(int, int);
void displayMenu(int&);
std::string getWord();
int main()
{
std::string originalWord{};
bool keepLooping{ true };
int choice{};
while (keepLooping)
{
displayMenu(choice);
switch (choice)
{
case 1:
std::cin.ignore(std::numeric_limits < std::streamsize >::max(), '\n');
originalWord = getWord();
keepLooping = true;
break;
case 2:
std::cout << "Reverse Word: " << reverse(originalWord) << std::endl;
keepLooping = true;
break;
case 3:
std::cout << "Randomized Word: " << randomizeWord(originalWord) << std::endl;
keepLooping = true;
break;
case 4:
std::cout << "Uppercase Word: " << upperCase(originalWord) << std::endl;
keepLooping = true;
break;
case 5:
std::cout << "Lowercase Word: " << lowerCase(originalWord) << std::endl;
keepLooping = true;
break;
case 6:
std::cout << "Alphabetized Word: " << alphabetize(originalWord) << std::endl;
keepLooping = true;
break;
case 7:
std::cout << "Closing the program...\n";
keepLooping = false;
break;
default:
std::cout << "Please choose between 1-7\n";
keepLooping = true;
}
std::cout << "Original Word: " << originalWord << std::endl;
}
}
/*
This function takes the original word from the user
and reverse it by using the for loop with the counter
set to the size of the word from the user and will save
each single character into the result variable.
*/
std::string reverse(const std::string originalWord)
{
std::string result{ originalWord };
std::size_t increment{ 0 }; // Counter to be used as increment for each char it takes.
for (std::size_t count = originalWord.size(); count > 0; count--)
{
result[increment] = originalWord[count - 1];
increment++;
}
return result;
}
std::string randomizeWord(const std::string originalWord)
{
std::string tempWord{ originalWord }; // Save the user inputted string into a temp to be manipulated.
std::string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
std::string result{}; // To save the randomize word and return it back to the function caller.
while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
{
constint randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
result.append(singleCharacter); // Append the character into the result string.
tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
}
return result;
}
void displayMenu(int &choice)
{
std::cout << "\n String Manipulator\n";
std::cout << "-----------------------------------------\n";
std::cout << "1) Enter the word\n";
std::cout << "2) Reverse the word\n";
std::cout << "3) Randomize the word\n";
std::cout << "4) Upper case the word\n";
std::cout << "5) Lower case the word.\n";
std::cout << "6) Alphabetical order the word.\n";
std::cout << "7) Exit the program\n\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
}
/*
This function is to obtain the word from the user.
It uses the getline to obtain multiple words with
spaces (i.e. Programming is fun!). It will return
the word back to function caller.
*/
std::string getWord()
{
std::string word{};
std::cout << "Enter a word: ";
std::getline(std::cin, word);
return word;
}
/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> distrib(min, max);
constint random = distrib(rng);
return random;
}
/*This function takes the word inputted by the user
and uses a for loop to iterate each character
and lower the case to be saved into a result variable
to be return to the caller function.
*/
std::string lowerCase(const std::string originalWord)
{
std::string result{ originalWord };
for (std::size_t count = 0; count < result.size(); count++)
{
result[count] = std::tolower(originalWord[count]);
}
return result;
}
/*This function takes the word inputted by the user
and uses a for loop to iterate each character
and upper the case to be saved into a result variable
to be return to the caller function.
*/
std::string upperCase(const std::string originalWord)
{
std::string result{ originalWord };
for (std::size_t count = 0; count < result.size(); count++)
{
result[count] = std::toupper(originalWord[count]);
}
return result;
}
/*
This function uses the bubble sort algorithm.
Inside the function is a for loop nested inside a
do-while loop. The for loop sequences through the
entire array, comparing each element with its neighbor
and swapping them if necessary. Anytime two elements
are exchanged, the flag variable swap is set to true.
The for loop is executed repeatedly until it can
sequence through the entire array
without making any exchanges.
*/
std::string alphabetize(const std::string originalWord)
{
bool swap;
std::string result{originalWord};
char temp{}; // To save characters to swap the letters.
do
{
swap = false;
for (std::size_t count = 0; count < result.size() - 1; count++)
{
if (result[count] > result[count + 1])
{
temp = result[count];
result[count] = result[count + 1];
result[count + 1] = temp;
swap = true;
}
}
} while (swap);
return result;
}
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#include <cctype> // Needed for toupper and tolower
usingnamespace std;
// Prompt for a new word and return it.
// Print a "thanks for playing" message
//void
void QuitNow(/*void*/)
{
cout << endl << endl << "Thank you for using StringWorld - please come back soon" <<
endl;
}
// Reverse a string
//string
string ReverseWord(string originalString)
{
string localString;
int len;
cout << "Original String: " << originalString << "\n";
len = originalString.length();
cout << "Total Length: " << len << "\n";
for (int i = len; i >= 0; i++) {
cout << "now lets see what localString contains: " << localString << "\n";
}
return localString;
}
// Convert the string to upper case and return the new string
//string
string stringToUpper(string currentString)
{
string result{ currentString };
for (unsignedint count = 0; count < result.size(); count++)
{
result[count] = toupper(currentString[count]);
}
return result;
//cout << "StringToUpper is not yet implemented\n";
//return currentString;
}
// Convert the string to lower case and return the new string
//string
string stringToLower(string currentString)
{
string result{ currentString };
for (unsignedint count = 0; count < result.size(); count++)
{
result[count] = tolower(currentString[count]);
}
return result;
//cout << "StringToLower is not yet implemented\n";
//return currentString;
}
/*
This function uses the random library to obtain
a random number between 0 and the size of the
string minus one.
*/
int randomNumber(int min, int max)
{
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> distrib(min, max);
constint random = distrib(rng);
return random;
}
// Randomize the string and return the randomized string
//string
string randomizeString(string currentString)
{
string tempWord{ currentString }; // Save the user inputted string into a temp to be manipulated.
string singleCharacter{}; // To be use to obtain single characters to append to the result string later.
string result{}; // To save the randomize word and return it back to the function caller.
while (!tempWord.empty()) // As long as the temp string is not empty, keep looping.
{
constint randomNum{ randomNumber(0, tempWord.size() - 1) }; // Obtain the random number.
singleCharacter = tempWord.at(randomNum); // Random the location of the temp string and save the character.
result.append(singleCharacter); // Append the character into the result string.
tempWord.erase(tempWord.begin() + randomNum); // Delete the used character to prevent reuse.
}
return result;
//cout << "randomizeString is not yet implemented\n";
//return currentString;
}
// Sort the letters in the string and return the sorted string
//string
string sortLetters(string currentString)
{
bool swap;
std::string result{ stringToLower(currentString) }; // Convert to lower case to sort the letters.
char temp{}; // To save characters to swap the letters.
do
{
swap = false;
for (std::size_t count = 0; count < result.size() - 1; count++)
{
if (result[count] > result[count + 1])
{
temp = result[count];
result[count] = result[count + 1];
result[count + 1] = temp;
swap = true;
}
}
} while (swap);
return result;
//cout << "sortLetters is not yet implemented\n";
//return currentString;
}
void Menu(/*void*/)
{
string currentString;
int menuOption;
bool quit = false;
cout << "Welcome to StringWorld" << endl << endl;
do {
// Print the current word and the menu
cout << "\t1. Reverse the current word or phrase" << endl;
cout << "\t2. Convert the current word to uppercase" << endl;
cout << "\t3. Convert the current word to lowercase" << endl;
cout << "\t4. Randomize the letters in the current word." << endl;
cout << "\t5. Sort the letters in the current word in ascending alphabetic order." << endl;
cout << "\t0. Quit StringWorld" << endl;
// Get teh user's selection
string word;
cout << "Enter a word: ";
cin >> word;
cout << "Please enter a valid option (1 - 5 or 0 to quit): ";
cin >> menuOption;
// Switch on the user's selction and do the right thing.
switch (menuOption) {
case 1:
currentString = ReverseWord(word/*currentString*/);
cout << "Reversed Word: " << currentString << endl;
break;
case 2:
currentString = stringToUpper(word/*currentString*/);
cout << "Uppercase Word: " << currentString << endl;
//while (str[i])
//{
// c = str[i];
// putchar(toupper(c));
// i++;
//}
break;
case 3:
currentString = stringToLower(word/*currentString*/);
cout << "Lowercase Word: " << currentString << endl;
//while (str[i])
//{
// c = str[i];
// putchar(tolower(c));
// i++;
break;
case 4:
currentString = randomizeString(word/*currentString*/);
cout << "Randomized String: " << currentString << endl;
break;
case 5:
currentString = sortLetters(word/*currentString*/);
cout << "Sorted String: " << currentString << endl;
break;
case 0:
QuitNow();
quit = true; // indicate that you should quit
break;
}
} while (!quit);
}
int main()
{
Menu();
return 0;
}
I didn't fix it on purpose. My advise is to work on it and if you have any question, ask away. You can take a look at my example code I posted it to give you an idea.
I do not know what you updated in the function below, but I wrote some comments with some things to keep in mind.
1 2 3 4 5 6 7 8 9 10 11 12
string ReverseWord(string originalString)
{
string localString; // Nothing is assigned to this variable.
int len;
cout << "Original String: " << originalString << "\n";
len = originalString.length();
cout << "Total Length: " << len << "\n";
for (int i = len; i >= 0; i++) {
cout << "now lets see what localString contains: " << localString << "\n"; // It will display every loop and nothing is assigned to this variable.
}
return localString; // Since nothing is assigned to this variable, then the return is undefined or blank.
}
I can see your point. I got other obligations; therefore, I do not answer to homework related PMs. My reason is that beginners can benefit from it and others with more experienced can provide their valuable input. I recommend you to do a internet search about reversing strings or something in those terms. You should be close to finish. Happy coding!
As a long term member of this site I'm just offering some friendly advice. Take it or leave it.
I have been keeping an eye on this and wrote a bit of code for fun. I didn't want to side track your conversation with chicofeo but was going to suggest you keep the switch a bit neater and smaller by calling functions from it. Keeping it small makes it easier for the reader to comprehend the structure.
Hi am new to this.
Just wanted someone to help me with my assignment to asks me to do:
Make a menu
1:Enter a word.
2:Reverse word than been enetered.
3:Randomise the word.
4:Upper case the word
5:Lower case it
6:Organise in the alapabetical order of the word.
Exit.
It has to have a menu where the user renter a word then presses a number for example 2 then the program
reverses the word etc.
Thanks looking forward for the replies.
And yea I have started it and got basic codes done but am struggling on writing codes that aren’t from
online(plagiarism reasons)