Click to See Complete Forum and Search --> : String Manipulation
Shasta80
May 13th, 2006, 10:56 AM
Hello,
I am trying to learn C++ and running into a problem that I"m at a loss how to get started or basically finished.
Ok I am building a small C++ console app that takes input from a user and then randomly displays it back with the characters all mixed up.
I created a character array (ex. char str[21]) for the input using cin.getline but how do I mix up the input and is it possible to remove any spaces the user may have put in? I've been reading online tutorials but they more or less capture input and display exactly what has been entered.
It's a real newb question, I know. I just need that bump start or direction.
Thank you.
JamesSchumacher
May 13th, 2006, 11:28 AM
#include <string>
#include <cstdlib>
#include <ctime>
std::string MixUpCharacters(const std::string & strData)
{
const size_t nLength = strData.length();
std::string strRetValue;
std::set<int> arIndices;
srand(time_t(NULL));
int nTemp = 0;
while (arIndices.size() < nLength)
{
nTemp = rand() % static_cast<int>(nLength);
arIndices.insert(nTemp);
}
std::set<int>::const_iterator myIterator;
myIterator = arIndices.begin();
while (myIterator != arIndices.end())
{
if (strData[ *myIterator ] != ' ')
{
strRetValue += strData[ *myIterator ];
}
++myIterator;
}
return strRetValue;
}
Paul McKenzie
May 13th, 2006, 12:15 PM
Hello,
I am trying to learn C++ and running into a problem that I"m at a loss how to get started or basically finished.
Ok I am building a small C++ console app that takes input from a user and then randomly displays it back with the characters all mixed up.An alternate solution:
#include <string>
#include <algorithm>
std::string MixUpCharacters(std::string & strData)
{
std::random_shuffle( strData.begin(), strData.end() );
return strData;
}
Regards,
Paul McKenzie
Shasta80
May 13th, 2006, 01:07 PM
Wow thanks Paul and James. Will go try that. I guess if I include the Namespace std, i can remove the std:.
Thanks again
Shasta80
May 13th, 2006, 01:55 PM
I tried to work with those examples but I must be doing something wrong with passing strings and such, can't get it to compile.
Andreas Masur
May 13th, 2006, 02:29 PM
I tried to work with those examples but I must be doing something wrong with passing strings and such, can't get it to compile.
Well...what are the compile errors?
dcjr84
May 13th, 2006, 02:51 PM
I just compiled this, worked good for me.
I didn't even know the STL had a random_shuffle function! Thanks Paul :)
One small change though. I would pass the string by value instead
of by reference so the original string is not modified. Just in case
I want to use it later for some reason.
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
string MixUpCharacters(string strData);
int main(){
string myText = "Hello neighbor";
string newText = MixUpCharacters( myText );
cout << myText << endl;
cout << newText << endl;
cin.get();
return 0;
}
string MixUpCharacters(string strData)
{
random_shuffle( strData.begin(), strData.end() );
return strData;
}
Shasta80
May 13th, 2006, 03:08 PM
Initalizing the variables seems to work as that example but trying it to use it with users input and using character arrays is a bit different. Let me go try something else and see..I need to be able to get rid of spaces as well only true characters.
Shasta80
May 13th, 2006, 03:42 PM
Well I got it too work like this but not sure if I am crossing any boundaries using a mixture of the char and string variable. But I still have to figure out to rmeove any spaces then build the string without any spaces.
Code:
#include <iostream
#include <string>
#include <algorithm>
using namespace std;
// This is where the array size is set.
const int intArSize = 20
string MixUpCharacters(string strData);
int main(int argc, char** argv)
{
//Variable to hold initial input
char charInitInput[intArSize];
// Display the welcome text
cout << "Welcome to the text randomizer. This program will take the user's input" << "\n";
cout << "and display the characters of the input in a random fashion" << ".\n";
cout << "Have Fun!" << "\n\n";
// Display
cout << "Enter your text to a maximum of " << intArSize << ".\n" << "\n\n";
// The function 'getline' will read the entire inputted text
// until user hits 'Enter'. The amount of characters read is
// dependant on the array size, spaces are included as 1 character.
cin.getline(charInitInput, intArSize);
string newStr = MixUpCharacters (charInitInput);
//Displays the initial input
cout << "Your initial input was " << charInitInput << ".\n" << "\n";
cout << "The randomized version of your input is " << newStr << "\n\n";
cout << "Press Any Key To Exit.";
cin.get();
}
string MixUpCharacters(string strData)
{
random_shuffle( strData.begin(), strData.end() );
return strData;
}
dcjr84
May 13th, 2006, 06:51 PM
Why even bother with the char array?
Just read the input into a string variable
using the getline() function.
Like this...
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string MixUpCharacters(string strData);
int main(int argc, char* argv[])
{
//Variable to hold initial input
string inputText;
// Display the welcome text
cout << "Welcome to the text randomizer. This program will take the user's input" << "\n";
cout << "and display the characters of the input in a random fashion" << ".\n";
cout << "Have Fun!" << "\n\n";
// Display
cout << "Enter your text: ";
// The function 'getline' will read the entire line of
//input text until user hits 'Enter'.
getline(cin, inputText);
string newStr = MixUpCharacters(inputText);
//Displays the initial input
cout << endl;
cout << "Your initial input was " << inputText << endl;
cout << "The randomized version of your input is " << newStr << endl;
cout << "Press Any Key To Exit.";
cin.get();
return 0;
}
string MixUpCharacters(string strData)
{
random_shuffle( strData.begin(), strData.end() );
return strData;
}
Shasta80
May 13th, 2006, 09:36 PM
hmm Good question...But I have added some other stuff and 'strlen' does not work on the variable being a string.
hmmm. I just used str.length()
dcjr84
May 13th, 2006, 11:34 PM
Yes, this is the beauty of using the std::string :)
You don't need to know or keep track of how long the string is
because that information is all stored automatically within the
string itself and can be retrieved using the length() member function.
This is why using strings is recommended over char arrays whenever
possible.
No need for the strlen() function with strings.
Shasta80
May 14th, 2006, 10:49 AM
Really appreciate everyones help.
Shasta80
May 14th, 2006, 07:08 PM
Have a bit of an issue with this program. Firstly because I'm using the random function I need at least 4 characters to seed so I put a loop in the input to only continue when 4 or more characters are entered. The problem is if you hit the space bar, that counts as a character. So hitting the space bar 4 times ends up with a blank result of course.
Anyway to disable the space bar during input?
is.chris
May 15th, 2006, 12:21 AM
Have a bit of an issue with this program. Firstly because I'm using the random function I need at least 4 characters to seed so I put a loop in the input to only continue when 4 or more characters are entered. The problem is if you hit the space bar, that counts as a character. So hitting the space bar 4 times ends up with a blank result of course.
Anyway to disable the space bar during input?
stopping a space being entered in cin is something I have never looked at but anyway if you captured each character as the user enters the string you can disregard spaces and copy all good characters into a buffer.
I know there is a function called getch() which returns the byte value of each character, not sure which header to include tho.
in a loop you could retrieve characters untill the enter key is pressed or something. and simply disregard spaces
char c_Return = getch();
if( c_Return != char( ' ' ) ){
//Add character to buffer
}
dcjr84
May 15th, 2006, 05:17 AM
Shasta80,
Post your code so we can have a look :)
Also post any errors you may be getting: compile-time or run-time
Remember to use code tags (http://www.codeguru.com/forum/misc.php?do=bbcode) please
exterminator
May 15th, 2006, 05:32 AM
Depends on how the user enters data or how you expect him to enter data. If it is important to capture each and every character the person enters - take input character wise and ignore the whitespaces. But if it is not a problem to take in the complete input string, then I would go ahead with getline and remove the un-wanted characters later in one go.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.