A function to reverse a string and return it...

This is a discussion on A function to reverse a string and return it... within the C Programming forums, part of the General Programming Boards category; An array isn't a pointer in c. You can't write something like char s[180]; ++s; and sizeof s returns 180 ...

  1. #16
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    An array isn't a pointer in c. You can't write something like

    char s[180]; ++s;
    and sizeof s returns 180

    Any time you pass an array into a function it decays into a pointer.

  2. #17
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    OK, thanks for clearing that up - I misunderstood

  3. #18
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Deckard, thnx for your code. But my goal is to write a recursive function, i already knew the iterative way, but i realy find difficult in using recursive functions in this .

  4. #19
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    YES YES YES i finally got it, worked out the solution for palindrome, the key was just to add 2 more arguments to the function. Here's the code and thnx for all help from start to finish.

    Code:
    #include <stdio.h>
    
    #define SIZE 6
    
    int palindrome( char string[], int start, int end );
    
    int main()
    {
       char string[ SIZE ] = "radar";
    
       if ( palindrome( string, 0, SIZE - 2 ) == 1 )
          printf( "It IS a palindrome.\n" );
       else
          printf( "It is NOT a palindrome.\n" );
    
       getch();
       return 0;
    }
    
    int palindrome( char string[], int start, int end )
    {
       int length = 0;
       int i = 0;
    
       if ( start == end )
          return 1;
    
       while ( string[ i++ ] != '\0' )
          length++;
    
       if ( length - 1 == 1 || length - 1 == 0 )
          return 1;
       else if ( string[ start ] != string[ end ] )
          return 0;
       else
          return palindrome( string, start + 1, end - 1 );
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21