Thread: Randomizing a person's name

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Randomizing a person's name

    Hello,

    I'm using some of the lines of code from the tutorials on strings but I have a question.


    Is it possible to take the user's entry of first and last names and shuffle it? Including the location of where the space is?

    A finger pointing towards the direction of my answer would be most appreciated.

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    One possibility would be to use the C strtok() function to separate the strings out into the first and last names:
    Code:
    /* Assume the first and last name is stored within a C++ string, temp_str */
    temp_ptr = strtok(temp_str.c_str(), " \n");
    /* temp_ptr points to first name */
    temp_ptr = strtok(NULL, " \n");
    /* temp_ptr now points to surname */
    That isn't a very 'clean' way of doing it if you ask me - too much mixing of C with C++ for my liking. You could alternatively use the istream::getline() method. but parsing the string using strtok is probably the single easiest way of doing it.

    Once you have the individual first and last names of a person, shuffling them is easy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomizing arrays!!
    By vulock_ka in forum C Programming
    Replies: 15
    Last Post: 08-02-2010, 11:52 AM
  2. Randomizing dealing program C
    By BSmith4740 in forum C Programming
    Replies: 2
    Last Post: 08-04-2008, 01:42 PM
  3. randomizing
    By zanderela in forum C Programming
    Replies: 2
    Last Post: 03-21-2008, 01:54 AM
  4. searching a person's HD
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 04-10-2003, 05:46 AM
  5. persons and people
    By Sekti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-26-2002, 03:09 AM