Reversing character array without accessing thro' index.

This is a discussion on Reversing character array without accessing thro' index. within the C Programming forums, part of the General Programming Boards category; Hello, I want to reverse array without using indexing in the array. Here is the code developed. Please let me ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Reversing character array without accessing thro' index.

    Hello,

    I want to reverse array without using indexing in the array. Here is the code developed. Please let me know, if anybody can suggest better idea.

    Code:
    # include <stdio.h>
    
    void print_rev(void);
    void print_char(char *pointer);
    
    char array[]={'A','B','C','D','E','F','G','H','I','J','K'};
    int sizeofarray;
    
    main()
    {
    	print_rev();
    }
    
    void print_rev(void)
    {
    	char *cPointer = array;
    	sizeofarray = sizeof(array)/sizeof(array[0]);
    	print_char(cPointer);
    	printf("%c",*cPointer);
    }
    
    void print_char(char *pointer)
    {
    	if (sizeofarray--)
    	{
    		pointer = pointer +1;
    		print_char(pointer);
    	}
    	printf("%c",*pointer);
    }
    Following version of print_char does not work. Pointer not advancing.. Any idea.

    Code:
    void print_char(char *pointer)
    {
    	if (sizeofarray--)
    	      print_char(pointer++);
    	printf("%c",*pointer);
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    You'd need to use ++pointer instead of pointer++.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Would you consider this indexing?

    Code:
    void print_rev(void)
    {
      char *cPointer;
    
      sizeofarray = sizeof(array)/sizeof(array[0]);
      cPointer = array+sizeofarray-1;
    
      while(cPointer >= array)
        printf("%c", *cPointer--);
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    I understand that pointer goes on stack prior to incrementing, so won't get correct ressult.
    Is this correct?

    And is there any way, I can modify print_char so that I don't have to use
    printf("%c",*cPointer); in print_rev function.

    Thanks,
    RT

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    This solved the problem.

    Code:
    void print_rev(void)
    {
    	char *cPointer = array;
    	sizeofarray = sizeof(array)/sizeof(array[0]);
    	print_char(cPointer);
    //	printf("%c",*cPointer);
    }
    
    void print_char(char *pointer)
    {
    	if (sizeofarray--)
    		print_char(pointer+1);
    	printf("%c",*pointer);
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Why even bother with two functions when one will do nicely?
    Code:
    #include <stdio.h>
    
    void print_rev ( const char *p )
    {
      if ( *p == '\0' )
        return;
      
      print_rev ( p + 1 );
      putchar ( *p );
    }
    
    int main ( void )
    {
      print_rev ( "\nABCDEFG" );
    
      return 0;
    }
    >main()
    This is a touchy subject. In C89 it's perfectly legal but considered bad style. In C99 it's illegal. The general consensus is to avoid using implicit int because it's either bad style or will result in a compiler error. Both are bad things. The preferred definition for main when taking no arguments is:
    Code:
    int main ( void )
    However, the void isn't required, so you could also legally do this:
    Code:
    int main()
    >print_rev();
    >}
    I see a disturbing lack of a return value. Even if you use implicit int, you still must return something or the behavior is undefined. I've found 0 to be a nice portable return value:
    Code:
    int main ( void )
    {
      print_rev();
    
      return 0;
    }
    >void print_char(char *pointer);
    This is fine, but if you don't plan on changing the contents of pointer, why not just make it const?
    Code:
    void print_char(const char *pointer);
    >char array[]={'A','B','C','D','E','F','G','H','I','J','K'};
    >int sizeofarray;
    Global variables should generally be avoided whenever possible. Passing arguments to functions is a more flexible solution anyway.

    >char array[]={'A','B','C','D','E','F','G','H','I','J','K'};
    This is my own preference, but I like to keep sequences of characters as strings for debugging and convenience purposes. The null character can be ignored in the code without much trouble, if any:
    Code:
    char array[] = "ABCDEFGHIJK";
    This can even be made const because you never change it. That way your code will be more robust.

    >int sizeofarray;
    Indices and array sizes are better suited as size_t variables simply because it's better style. You're also using sizeof, and it gives you a size_t. Using int isn't strictly wrong, per se, but it does raise warning flags for a knowledgeable reader.
    My best code is written with the delete key.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Do my eyes decieve me? PRELUDE YOU'RE BACK. /weeps in joy

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Welcome come back ma'am

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Welcome back Prelude..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Puzzled.
    By silhoutte75 in forum C Programming
    Replies: 13
    Last Post: 01-21-2008, 04:17 PM
  2. watching character array in visual studio
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2006, 11:12 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 12:12 AM

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