|
-
Jun 9th, 2005, 04:35 PM
#1
Thread Starter
Frenzied Member
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?
-
Jun 9th, 2005, 04:38 PM
#2
-
Jun 9th, 2005, 10:09 PM
#3
Addicted Member
Re: reverse string
 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.
-
Jun 10th, 2005, 06:03 AM
#4
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"
-
Jun 10th, 2005, 11:06 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|