Results 1 to 5 of 5

Thread: reverse string

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    reverse string

    Hi
    I am writing a small library, which I want to contain a method for reversing strings.

    I have the following in a header file:
    Code:
    // This header file contains function for all sorts of string manipulation.
    // In this first release, the three members of the string lib., is:
    // * Reverse string
    // * Parse for certain characters
    // * Seperate
    
    // The function to be created, is Reverse string.
    // This function will take a string as a parameter, and returns the reversed string.
    #include <string.h>
    #include <stdlib.h>
    #define MAX_STR_LEN 100
    
    
    SOMETYPE reverse_string(char string[MAX_STR_LEN])
    {
    	char revstring[MAX_STR_LEN];				// Reversed string
    	int len;									// Length of incoming string
    	int j;										// Counter variable
    
    	len = strlen(string);						// Get length of incoming string
    
    	for(int i = len; i >0; i--)					// Reverse the string
    	{
    		revstring[j] = string[i];
    		j++;
    	}
    	return ????????
    }
    In my main function I have a char string[MAX_STR_LEN] = "something"; and then call reverse_string(string);

    But ofcourse, the compiler complains about the types....how could I do this?
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: reverse string

    I think j can be replaced :

    Code:
    	for(int i = len; i >0; i--)					// Reverse the string
    	{
    		revstring[len-i] = string[i];
    	}


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    Re: reverse string

    Quote Originally Posted by CyberCarsten
    Code:
    SOMETYPE reverse_string(char string[MAX_STR_LEN])
    Your problem seems to be when you are the function reverse_string. You are are wanting to pass this function a character array but you've named it string! String is of course defined in string.h file so you need to change the name to something else and that should fix your problem.

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: reverse string

    You could also switch to C++ strings from the <string> header, they are in general a lot easier to use, especially since you don't have to worry about the maximum size.
    Code:
    #include <string>
    #include <algorithm>
    
    std::string str = "dlrow";
    str += " olleH";
    std::reverse(str.begin(), str.end()); // reverse the string
    // str == "Hello world"

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: reverse string

    But if you insist on using C-style strings, the usual way to return a string is to pass in a buffer and its size.

    Oh, and this:
    char * func(char string[MAX_STR_LEN])
    is, as far as the compiler is concerned, completely equivalent to
    void func(char *string)
    . In other words, the array length is simply ignored in function parameters. This means that the user can easily pass in a string that is longer than you expect and it will blow up in your face.

    Code:
    void strrev(char *dest, size_t destlen, const char *src)
    {
      const char *p;
      size_t srclen = strlen(src);
      for(p = src + (srclen >= destlen ? destlen - 2 : srclen - 1); p >= src; --p, ++dest) {
        *dest = *p;
      }
      *dest = '\0';
      return dest - (srclen >= destlen ? destlen : srclen + 1);
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width