a reverse function

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-27-2006
AngKar
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
• 04-27-2006
AngKar
Oops I used the == instead of =!
Ok that problem is solved!
But the second question remains...
Can anyone suggest me a generic logic?

AK
• 04-27-2006
willc0de4food
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..
Quote:

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.
• 04-27-2006
whiteflags
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 */ }```
• 04-27-2006
AngKar
Thanks a lot for the logic! But I may have strings too!Like
+a
-b
+c
.
.
.
What to do then? :(
• 04-27-2006
whiteflags
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.
• 04-27-2006
AngKar
Can't I just work on the strings?
• 04-27-2006
whiteflags
...Yes, but it's better if you use dataypes more responsibly. Programmers do not use char to hold numbers, ever, I think.
• 04-27-2006
AngKar
But I'm not holding numbers in char!
I've an array of strings on which I want to apply that reverse operation.
• 04-27-2006
willc0de4food
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
• 04-27-2006
AngKar
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!
• 04-27-2006
whiteflags
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>
• 04-27-2006
willc0de4food
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.
• 04-27-2006
AngKar
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; }```
But its not working!Could anyone please help me find out where I'm wrong!

AK
• 04-27-2006
AngKar
btw, i_data is a copy of data.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last