Thread: Reverse array of characters

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    Reverse array of characters

    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <conio.h> 
    #include <math.h> 
    
    void reverseAr(char ar[], int n); 
    
    int main() 
    { 
     int choice; 
     char *abc= ar[];
    
    switch (choice) { 
     case 1:
             reverseAr(ar);
         break;
    
    void reverseAr(char ar[], int n) 
    {
        char c;
        char *p, *q;
     
        p = ar;
        if (!p)
            return;
     
        q = p + 1;
        if (*q == '\0')
            return;
     
        c = *p;
        reverseAr(ar[]);
     
        while (*q != '\0') {
            *p = *q;
            p++;
            q++;
        }
        *p = c;
     
        return;
    
    }

    My codes keep couldn't get the reverse array of characters. Do anyone mind to tell me where is my mistake? Thank you in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks wrong:
    Code:
    char *abc= ar[];
    You probably want to declare ar, and maybe initialise it at the same time, e.g.,
    Code:
    char ar[] = "hello";
    This is wrong:
    reverseAr(ar);
    You declared reverseAr with two parameters, but you only passed it one argument when calling it.

    I suggest that you get rid of the choice variable and the switch. You might need it later, but for now it is just a distraction. As you do so, you also need to fix your main function such that it is properly defined (watch out for the closing brace).

    This is wrong:
    Code:
    reverseAr(ar[]);
    You probably want to call reverseAr, but whereas you passed ar correctly earlier, this time you do so wrongly, and you're still missing the second argument. In fact, since you have a loop later, I guess that this recursive call should be removed... but whether to do this or not depends on what is the algorithm that you have in mind.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    This code seems needlessly complicated. As laserlight pointed out there seems to be an odd mix of recursion and iteration. If you want to do it recursively stick to recursion and don't have the unrequired iterative loop (the recursive solution, by the way, will be an example of tail recursion which is easily converted to an iterative solution). I suggest that doing the recursive version first, and then converting this solution to an iterative solution might be valuable for learning.

    To help with the recursive solution, answering these questions may help...

    * If the string length is 1, how would this be reversed?
    * If the string length is 2, how would this be reversed?

    Another question:

    * If you want to swap the first character in a string with the final character in a string, how would you do this?

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    15

    Reverse array of characters

    I couldn't initialize the array as I have to get the array of characters from user input, hence I do not know how to do so.

    Code:
    void reverseAr(char ar[], int n) 
    { 	
       int i=0,j;
       char temp;
       
       int len = strlen(ar);
       if (n==0 || n==1)
          return;
       else
       {
    
    
    temp=*ar;
      *ar=ar[n-1];
      ar[n-1]=temp;
       }
      return reverseAr(ar+1,n-2);
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is continued in Reverse array of characters using recursive function.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. Take input characters and reverse
    By Passa in forum C Programming
    Replies: 2
    Last Post: 09-21-2010, 11:41 PM
  3. Replies: 34
    Last Post: 05-25-2010, 09:00 AM
  4. Help with Reverse Array
    By Crcullen3916 in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2007, 08:47 PM
  5. How do you reverse an array?
    By ducksickii in forum C Programming
    Replies: 25
    Last Post: 04-01-2007, 05:39 AM

Tags for this Thread