Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Random_Shuffle with single space in the list of characters to shuffle C++  (Read 43447 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Been playing around with Random_Shuffle again and trying to figure out a way to add a single space into the string to shuffle it as a single character space that can end up in any element location as a result of the random_shuffle. The C++ instructions that I am working with are like the following. But there is no escape character sequence that I can find to add a single space to the string to be shuffled.

I found a reference to isspace which could be used for a character substitution in place of space such as space detected pass a character as ö for space in the input string that works with this shuffle below if ö was added to the string to shuffle to pair up as a character match. https://www.geeksforgeeks.org/isspace-in-c-and-its-application-to-count-whitespace-characters/

Quote
string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;";
   
random_shuffle(str.begin(), str.end())

Wasn't sure if maybe an unusual to English character may need to be used such as ö added to the list for substitution for space, and ö is used for a space translation in the output, or if there is a method to add the ASCII equivalent to space character in the string above such as ASCII code 0x20. Been trying a few things and compiler not happy with trying to force it to accept a single space to the string str. I haven't gone through the method yet of adding an ö to the string to be shuffled and perform a space to ö translation yet because I have been known to do things the hard way and perhaps there is a better way than a character substitution, however if the character substitution method if the best way to get it to accept adding a space to the shuffle, then i can go that route.  :)

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Random_Shuffle with single space in the list of characters to shuffle C++
« Reply #1 on: November 22, 2019, 06:38:19 PM »
Unless I am misunderstanding, does
Code: [Select]
string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,; ";

That not work? Just adding a space to the end of the string itself?
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Random_Shuffle with single space in the list of characters to shuffle C++
« Reply #2 on: November 22, 2019, 07:17:39 PM »
Thanks for help with this BC. That's the one thing I didnt try was a space without an escape character.  :-[

That fixed the compile issue, however now trying to figure out a way to fix another part of program that broke. The part that broke looks at the length of the string that is input and it seems that the first space that is entered it thinks is the end of the string and so a string of ABC DE it thinks is 3 characters in length long instead of 6, so the space entry confuses the string length instruction that I have. It measures proper length of characters when all bound tight without spaces, so curious how to fix this string length issue where a single space is seen as end of string?  :-\

Here is more code snippet to show what I have going on: ( location=i-1; is used to point to the correct array element where arrays start with first element at 0. )
Also hoping its not confusing having a lot going on with little commenting. I had to print it out and write on paper with colored pens showing whats passed to what to keep track of it where values are passed between arguments of functions as its slightly spaghetti.  :)

Quote
    cout<<"Enter String\n";
    string MyString="";
    cin>>MyString;
    string s(MyString);
    char p[s.length()];

    int i;

    // assigning value to string s
    int n = s.length();

    // declaring character array
    char user_array[n + 1];

    // copying the contents of the
    // string to char array
    strcpy(user_array, s.c_str());

    for (int X = 0; X < n; X++){
        //Pass to Char Array
    }

    for (i = 0; i < sizeof(p); i++) {
        p = s;
    }

    location=i-1;

    string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm nopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;{}[]'|\"` ";

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Random_Shuffle with single space in the list of characters to shuffle C++
« Reply #3 on: November 22, 2019, 07:42:48 PM »
reading a string from standard input via the << operator only reads to the first whitespace character, so the length is not incorrect- the string is not what you expected.

If you want to read an entire line you can use std::getline.

Code: [Select]
getline(cin, MyString);
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Random_Shuffle with single space in the list of characters to shuffle C++
« Reply #4 on: November 22, 2019, 07:45:04 PM »
Sweet! Thanks for the help BC.  :)  And brought your thanked to 1111  ;D