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

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

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

    Hi

    i need to write a function to reverse a string and return it, and i have to do it recursively. I can only write it to print out the reversed string but can't use recursion and return the actual reversed string. I have no idea on how to do it. Pls use some pseudocode or code snippets to help me.

    Thnx in advance

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    char str[] = "Hello World";
    
    printf("%s",strrev(str));
    return 0;
    }
    Is that what you want?

    Or do you have to write the function from scratch?

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    i want to write it from scratch, not using any special functions. Pls help me.

    thnx a lot

  4. #4
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    im guessing you would read the string character by character and place each character into an array. Then read the array members in reverse order concatenating* each character onto the end of a new string.


    *fastening on to the end of the string ( i think its the correct term - im not completely awake yet!)

    hope this helps
    Monday - what a way to spend a seventh of your life

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Okay....quick and dirty.....

    Code:
    #include <stdio.h>
    #include <string.h>
    char* Rev(char *str);
    int main(int argc, char *argv[])
    {
    char str[] = "Hello World";
    char str2[] = "Programming Rules!" ;
    
    printf("First string %s\n",str);
    printf("Second sring %s\n",str2);
    printf("First string reversed %s\n",Rev(str));
    printf("Second sring reversed %s\n",Rev(str2));
    printf("First string reversed back %s\n",Rev(str));
    printf("Second sring reversed back %s\n",Rev(str2));
    return 0;
    }
    
    char* Rev(char *str){
     int x = strlen(str);
     char temp;
     int y ,z;
     if(x % 2){
     for(y = 0, z = x-1; y != z; y++, z--){ // If even length string
     temp = str[y];
     str[y] = str[z];
     str[z] = temp;} }
     else {
     for(y = 0, z = x-1; y+1 != z; y++, z--){// If odd length string
     temp = str[y];
     str[y] = str[z];
     str[z] = temp;} }
     return str;
     }
    This seems to work....but I cant guarantee....

    I tested it with an even length string and an odd length string and its seems happy.....

    The array is passed just like a pointer, therefore using this function alters your data (not often too good )

    Try test this more and add any refinements......I cant test it properly as I wrote it while on break at work so dont yell if its goes wrong
    Last edited by Fordy; 01-11-2002 at 12:31 PM.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    hi , thnx to fordy for providing the code. The sad thing is, i'm not suppose to use pointers or other other string manipulation functions to help me, suppose to write from scratchl. but i appreciate it.

    To iain, i thought you what you posted, but how do you return it? You can't return an array right?

    thnx

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > You can't return an array right?

    Not if you can't use pointers - the beginning of an array's just a pointer

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Can someone pls give me a recursive solution of the Palindrome problem.

    thnx

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>The sad thing is, i'm not suppose to use pointers or other other string manipulation functions to help me

    What!!! no pointers????.....that's what strings are...a pointer to the first element of the array....

    Ah well......I cant help any more right now cuz I cant easilly envision what you are supposed to do......and I havent got time to think it through......nevermind....

  10. #10
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    you cant use pointers or string manipulation!! In that case i dont think its possible without using assembly. The only way to return an array is to return a pointer to it. ill think about it again..
    Monday - what a way to spend a seventh of your life

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    i dont' mean i havce to return an array. My goal is to write a palindrome program that test a string if it's a palindrome and i can't use pointers or other string manipulation functions thats all.

    i gave it a go in a new thread, but it doesn' work.

  12. #12
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I'm just going to point out that strrev
    is not a standard c function.

  13. #13
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Actually you could do this without pointers
    if you pass in

    struct s {
    char s[180];
    };

    void reverse_str(struct s s);

  14. #14
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > Actually you could do this without pointers

    But since the array's a pointer, wouldn't that still count as "using pointers"?

  15. #15
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    This code works without the use of pointers. If my use of strlen() violates the restriction on using string manipulators, you could either initialize the variable 'end' (in is_palindrome()) to 4, or you could modify the code to pass in the length of the word to check.

    Code:
    #include <stdio.h>
    
    int is_palindrome( char s[] );
    
    int main(void)
    {
      char s[] = "RADAR";
     
      if ( is_palindrome(s) )
        printf( "Sure enough\n" );
      else
        printf( "Nope, nada, zip, zilch\n" );
     
      return (0);
    }
     
    int is_palindrome( char s[] )
    {
      short   pally = 1,
              start = 0,
              end   = strlen(s) - 1;
     
      while ( start < end )
      {
        if ( s[start++] != s[end--] )
        {
          pally = 0;
          break;
        }
      }
      return (pally);
    }
    Last edited by Deckard; 01-11-2002 at 04:48 PM.
    Jason Deckard

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, 04:13 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07: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