Thread: Passing Values Question... (continue)

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    27

    Unhappy Passing Values Question... (continue)

    Hey... thank you all for your help.

    I tried to fix my code. Just as a reminder, this was the question:
    The following program segmant asks the user for his or her first name in main(). Write a function that reverses the name in the array so that main() can print the reversed name. To make it easy, assume that the user name is always five characters long ("Jerry" or "Peter", for example).

    This is my new code:
    Code:
    #include <iostream>
    using namespace std;
    
    rev(char name[6]);
    
    main()
    {
         char name[6];
         cout << "What is your first name (5 letters, please)? ";
         cin  >> name;
    
         rev(name);
         for(int a=0; a<5; a++)
    {
    cout<<name[a];
    }
         return 0;
    }
    //from here is what I added
    rev(char name[6])
    {
    	char temp;
          temp = name[0];
          name[0] = name[4];
          name[4] = temp;
    
    	  temp = name[1];
          name[0] = name[3];
          name[3] = temp;
           
          return 0;
    }
    How come it doesn't work? Not all the characters reverse.

    Thanks, and looking forward for the answer...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't do anything with name[5].

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void rev(char *s) {
        int x, y, t;
    
        /* find length of string */
        for(x = 0; s[x]; x ++) ;
        x --;
    
        for(y = 0; x > 0; x--, y++) {
            t = s[y];
            s[y] = s[x];
            s[x] = t;
        }
    }
    Some compilers include a function called strrev() that reverses a string.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >rev(char name[6]);
    C++ no longer guarantees that rev will return int. In fact, a correct implementation will fail to compile unless you specify and explicit type.

    >main()
    Implicit int is no longer valid C++.

    >rev(char name[6])
    Noticing a pattern? I am.

    Rather than try to do this all in line on the assumption that the string is only 5 characters long, it's easier to write a generic function for any length string. Just start at each end of the array and swap the characters until the indices cross:
    Code:
    for ( i = 0, j = std::strlen ( s ) - 1; i < j; i++, j-- )
      std::swap ( s[i], s[j] );
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How come it doesn't work? Not all the characters reverse.
    Get another piece of paper out. Write down six column headings and a 'temp' column heading:

    a[0], a[1], a[2], a[3], a[4], a[5], temp

    Mentally enter a name as the input. Write each character of the name under the proper column heading--remember after the last letter in the name, the next column will contain a \0' character. Then, perform these operations:
    Code:
    temp = name[0];
    name[0] = name[4];
    name[4] = temp;
    
    temp = name[1];
    name[0] = name[3];
    name[3] = temp;
    For every statement, you should be crossing out an old value under a column heading and writing down a new value.

    As a further observation, what do the lines in red do?
    Code:
    temp = name[0];
    name[0] = name[4];
    name[4] = temp;
    
    temp = name[1];
    name[0] = name[3];
    name[3] = temp;
    Last edited by 7stud; 06-29-2005 at 01:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  2. Help passing and storing
    By devilsknight in forum C Programming
    Replies: 7
    Last Post: 07-03-2006, 03:20 AM
  3. Passing Template values on to other classes
    By vinsta18 in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2004, 05:26 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. passing values in array
    By xenodvs1 in forum C Programming
    Replies: 3
    Last Post: 02-12-2003, 01:50 PM