1. ## a reverse function

Hi,

Say,my data is as follows:
+1
+2
-3
+4
-5

What I want to do is following:

I want to write a reverse function that will look like:

reverse (char *data[],pos1,pos2)

An example to illustrate this fn is:

Say I call reverse (data,0,3).

Then my o/p will be +3 -2 -1 +4 -5 i.e. it will not only reverse the digits between the 2 positions but also the signs.

Firstly I'm trying this with what if pos2-pos1 =1 then I need to change the sign of only data[pos2][0]. I'm writing:
Code:
```	if (abs(pos2-pos1)==1)
{
if(data[pos1][0]=='+')
data[pos1][0]=='-';
else
data[pos1][0]=='+';
}```
But strangely even if I check that the difference is 1 the fn is not entering the if loop!!!
Any idea why this can happen?
Also what should be a better approach (so that I dont need to consider the difference = 1 case)

Thanks,
Angkar

2. Oops I used the == instead of =!
Ok that problem is solved!
But the second question remains...
Can anyone suggest me a generic logic?

AK

3. why dont you just use an int? if it's less than zero, multiply it by negative one. if its greater than zero, multiply it by negative one. after you do that all you have to do is put the numbers in the order that you want...

also..
if (abs(pos2-pos1)==1)
{
if(data[pos1][0]=='+')
data[pos1][0]=='-';
else
data[pos1][0]=='+';
}
that doesn't work. it would be
Code:
`data[pos1][0] = '-';`
you're not trying to compare anything, you're trying to assign something.

4. My suggestion would be to simplify your life. Positive and negative are part of the nature of numbers, you don't need to complicate things by bringing in strings. There is a bit in the int type dedicated to holding the sign of a number. So use an integer array or whatever.

Code:
```int reverse (int data[], size_t pos, size_t pos2)
{
/* just swap  and times by a negative one to change the sign */
int temp = data[pos] * -1;
data[pos] = data[pos2];
data[pos2] = temp;

return data[pos]; /* return the value that changed */
}```

5. Thanks a lot for the logic! But I may have strings too!Like
+a
-b
+c
.
.
.
What to do then?

6. Uh well that depends, if you are coding a program to do algebra I guess you would have to do this, BUT if you're just getting numbers from the user:

- convert the string data to a numeric type with strtol()
- shove it in a long array
- use the function, but make it work with long parameters and a long return type.

Since In don't know where or why positive or negative char would matter.

7. Can't I just work on the strings?

8. ...Yes, but it's better if you use dataypes more responsibly. Programmers do not use char to hold numbers, ever, I think.

9. But I'm not holding numbers in char!
I've an array of strings on which I want to apply that reverse operation.

10. phone numbers. lol addresses...birthdays. there would be no point in having something like 3 ints for the mo / day / yr of a birthday and then outputting the 3 int's with the slashes in between. itd be much easier to store such data in a char array.

well if you're not holding numbers in a char and thats what you told us before, why dont you give us the actual data / code? lol

11. Thats great . But I think I've not made my problem clear!
I have to reverse (Along with signs) from pos to pos2 and not just pos and pos2!

Again thanks for showing the fn strpbrk. Never knew it!

12. Oh, well... that's a sorting algorithm, in part. I thought you just wanted to swap. You could use qsort() to solve the problem in part. Study up on it. It's a function in <stdlib.h>

13. iii think i get it now.
first, analyze the whole string and reverse the signs.
then go through and with the reversing, find out how far in to the string you need to go, and with a conditional i'd probably copy the information from the original string to a temp string so that it would be in a sorted manner without the data being overwritten. after you've sorted it, simply add the remaining part of the string to the temp string, and then output / do whatever with the temp string. you could copy it back over to the original string but that would be pointless as you already have the string in the format that you need it.

14. I've tried to write this:
Code:
```int start;
int end;
for (start = p1,end = p2-1;start<=end;start++,end--)
{
val = (char *) malloc(sizeof(data[end]));
val = rev_sign(data[end]);
strcpy(i_data[start],val);
val = rev_sign(data[start]);
strcpy(i_data[end],val);

}

char * rev_sign(char *ip)
{
if(ip[0]='+')
ip[0]='-';
else
ip[0]='+';
return ip;
}```