Thread: String Scrambler :)

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    49

    String Scrambler :)

    Hello,
    I know that algorithm has a shuffle function. Someone told me when I was making my program in a chat room lol. but I wanted to make it anways to see if I can do it. Please leave feed back and tell me what you think and improvements and stuff. Thanks. BYebye

    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    void Scramble(string &s){
    	int scram_num=s.size()*s.size();
        srand(time(NULL));
    	for(int a=0;a<scram_num;a++){
    
    int i=rand()%s.size();
    char c=s[i];
    s.erase(i,1);
    int ii=rand()%s.size();
    s.insert(ii,1,c);
    	}
    }
    
    
    int main(){
    string s="Country";
    Scramble(s);
    for(unsigned int i=0;i<s.size();i++){
    cout<<s[i];
    }
    	system("PAUSE");
    	return 0;
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Here's a few you may want to think about:
    Code:
    #include <iostream>
    #include <string>  //include this - I'm not sure how I compiled without it.
    //#include <time.h> //don't include this - .h standard headers have everything
    //in the global namespace which could lead to name collision
    #include <ctime> //include this one instead of <time.h>
    
    //using namespace std; //why bring the whole std namespace in?
    //You're only using two (soon to be three) things out of it
    using std::string;
    using std::cout;
    using std::endl;
    
    void Scramble(string &s)
    {
    	int size = s.size(); //create a temp variable for size
    	//instead of calling the function multiple times inside the for loop
    	int scram_num=size*size;
    	srand(time(NULL));
    	for(int a=0;a<scram_num;a++)
    	{
    		int i=rand()%size;
    		char c=s[i];
    		s.erase(i,1);
    		int ii=rand()%size;
    		s.insert(ii,1,c);
    	}
    }
    
    int main()
    {
    	string s="Country";
    	Scramble(s);
    	cout << s << endl;  //you can cout a string; make sure to flush the output
    
    	return 0;
    }
    I once wrote a shuffle like this:
    Code:
    #include <algorithm>
    
    void Shuffle(string &s)
    {
    	int size = s.size();
    	srand(time(NULL));
    	for(int a=0;a<size-1;a++)
    	{
    		int i = rand()%(size-a)+a;
    		while(i==a)
    			i = rand()%(size-a)+a;
    		std::swap(s[a],s[rand()%(size-a)+a]);
    	}
    }
    It keeps from erasing and inserting so much. It also cuts down on the number of times through the scramble loop.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Oh I saw you using Swap :P That is clever didnt think about that I was just thinking about inserting without changing any other places. Thanks for the insights

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM