locked
Reverse the input of a string. Visual Studio 2017 RRS feed

  • Question

  • For a school project I have the following code I have to write:

    Ask the user a first name, then a last name. Stop these values in two
    different arrays, of the right type. Use one or more loops of your choice first
    just print the name and then print the name in reverse order

    The following code I already have:

    int main ()
    {
    char first_name[20];
    char last_name [20];
    char total_name [100];
    char reversed [100];
    int counter = 0;

    printf ("Enter your first name:");
    gets_s (first_name);

    printf ("Enter your last_name:");
    gets_s (last_name);

    strcpy_s (total_name, first_name);
    strcat_s (total_name, "");
    strcat_s (total_name, last_name);

    while (counter == 0)
    {
    printf ("Your name is: %s", total_name);
    counter ++;
    }
    _getch ();
         return 0;
    }

    How do I reverse this string, so that when I give in Dave Jaski it gives iksaJ evaD in return.

    thanks!




    • Edited by nina1712 Sunday, March 18, 2018 1:38 PM
    Sunday, March 18, 2018 1:16 PM

Answers

  • Thanks for your answer.

    the output shoot look like this:

    What is your name: Janssen
    What is your first name: Jan
    So you are Jan Janssen.
    In reverse order it is: nessnaJ naJ

    But I don't know how I have to reverse the input with the loops.

    The assignment requires that you store the data in arrays.  So how can you access the individual elements of an array to retrieve its value or store a different value to the array element?
    • Marked as answer by nina1712 Sunday, March 18, 2018 3:19 PM
    Sunday, March 18, 2018 3:15 PM

All replies

  • On 3/18/2018 9:16 AM, nina1712 wrote:

    Ask the user a first name, then a last name.Stop these values in two
    different arrays, of the right type. Use one or more loops of your choice first
    just print the name and then print the name in reverse order

    How do I reverse this string

    By using one or more loops of your choice, of course. The assignment seems clear.

    Sunday, March 18, 2018 2:22 PM
  • Thanks for your answer.

    the output shoot look like this:

    What is your name: Janssen
    What is your first name: Jan
    So you are Jan Janssen.
    In reverse order it is: nessnaJ naJ

    But I don't know how I have to reverse the input with the loops.

    Sunday, March 18, 2018 3:02 PM
  • Thanks for your answer.

    the output shoot look like this:

    What is your name: Janssen
    What is your first name: Jan
    So you are Jan Janssen.
    In reverse order it is: nessnaJ naJ

    But I don't know how I have to reverse the input with the loops.

    The assignment requires that you store the data in arrays.  So how can you access the individual elements of an array to retrieve its value or store a different value to the array element?
    • Marked as answer by nina1712 Sunday, March 18, 2018 3:19 PM
    Sunday, March 18, 2018 3:15 PM
  • Thanks. 

    I think i figured it out

    Sunday, March 18, 2018 3:20 PM
  • While you are at it, what is the purpose of the while loop?  Hopefully you realize it can never iterate more than once.
    Sunday, March 18, 2018 5:09 PM