Thread: reversing a string using pointers

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    reversing a string using pointers

    hello everyone,
    everything seems to be right in this program except that the puts() function does not print anything.please help!
    Code:
    /*c program to reverse a string using pointer*/
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
    	  char *str,*revstr;
    	  char astr[20],arevstr[20];
    	  int i,j;
    	  printf("\n Enter the string:");
    	  gets(astr);
    	  str=&astr[0];/*putting the address of astr in str pointer*/
    	  j=0;/*is required later in loop1*/
    	  revstr=&arevstr[0];/*putting the address of arevstr in revstr pointer*/
    	  for(i=strlen(astr);i>=0;i--)/*loop1:using pointers to put the contents of
    											  str in revstr in reversed manner*/
    	  {
    			 *(revstr+j)=*(str+i);
    			 printf("%c",*(revstr+j));/* seeing whether the assignment is correct*/
    			 j++;
    	  }
    	  for(j=0;j<=strlen(astr);j++) /*loop2:putting revstr pointer values on array
    											  arevstr*/
    	  {
    			 arevstr[j]=*(revstr+j);
    			 printf("%c",arevstr[j]);/*again seeing whether the assignment is
    												correct*/
    	  }
    	  arevstr[j]='\0';/*making arevstr a string*/
    	  printf("\n The reversed string is:");
    	  puts(arevstr);/*THE PROBLEM:nothing prints here*/
    	  getch();
    }

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    You can do this without a second array.

    Using your existing code though the problem is on line 15. i+strlen(astr) points to the strings terminating '\0' which you are then copying into the secondary array as its first character. So the secondary arrary's first character in '\0' which is an empty string.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Pulock2009 View Post
    everything seems to be right in this program except that the puts() function does not print anything.
    You should read FAQ before making such statements
    Mostly I put relevant links - but today I'm thinking reading through all level1 and 2 entries will be more educational. And I'm interested to see if this reading will have a desired influence on your code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    thanks a lot for your help!!!

    Quote Originally Posted by SirPrattlepod View Post
    You can do this without a second array.

    Using your existing code though the problem is on line 15. i+strlen(astr) points to the strings terminating '\0' which you are then copying into the secondary array as its first character. So the secondary arrary's first character in '\0' which is an empty string.
    thanks a lot for your help! immediately started working after making i=strlen(astr)-1. went nuts in the morning thinking what had gone wrong.
    thanks again!!

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    27
    thanks a lot for your suggestion. i will be going through the level1 and level 2 sections.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing Words and Array of Pointers
    By Striker87 in forum C Programming
    Replies: 3
    Last Post: 03-17-2011, 11:15 PM
  2. reversing a string, again..
    By Disident in forum C Programming
    Replies: 5
    Last Post: 06-15-2009, 08:01 PM
  3. reversing a string
    By swappo in forum C++ Programming
    Replies: 6
    Last Post: 06-14-2009, 03:18 PM
  4. Reversing a String in C
    By Ice in forum C Programming
    Replies: 6
    Last Post: 04-18-2002, 10:55 AM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM

Tags for this Thread