swapping array elements in revereser order

MrSalts

Joined Apr 2, 2020
2,767
Code:
#include <stdio.h>

int main()
{
    char name[]= {"OLLEH"};
    for (int i = 0; i < 2; i++){
        name[i] = name[i] + name[4 - i];
        name[4 - i] = name[i] - name[4 - i];
        name[i] = name[i] - name[4 - i];
    }
    printf("%s", name);
    return 0;
}
If the variable "i" that was used in the proposed solution is not allowed, we can do that too...

Code:
#include <stdio.h>

int main()
{
    char name[]= {"OLLEH"};
    name[0] = name[0] + name[4];
    name[4] = name[0] - name[4];
    name[0] = name[0] - name[4];
    name[1] = name[1] + name[3];
    name[3] = name[1] - name[3];
    name[1] = name[1] - name[3];
    printf("%s", name);
    return 0;
}
 
Last edited:

Thread Starter

Dadu@

Joined Feb 4, 2022
155
I will give you the solution:

Put A into TEMP
Put B into A
Put TEMP into B
I know I said earlier I want to get reverse string without separate array but I just changed my mind to save myself from big trouble and I am ready to use separate array now.

I have changed the original values of array by using sawping technique. I have decleared separate array temp.
When I compile code I get reverse string

C:
#include <stdio.h>

int main()
{
    int i = 0;
    int j = 4;
    char A[] = {"OLLEH"};
    char temp[5];
    
    for ( i = 0; i < 5; i++)
    {
        temp[j] = A[i];
        j--;
    }
    
    for ( i = 0; i < 5; i++)
    {
        A[i] = temp[i];
    }
    
    for ( i = 0; i < 5; i++)
    {
       printf("%c ",A[i]) ;
    }
    
    return 0;
}
H E L L O
Right now I have soultion for my problem but it doesn't seem enough to me. I know the length of string, so I didn't have much. trouble revesing it. I will try to write the program to reverse string for unknown length
 

click_here

Joined Sep 22, 2020
548
Another way of solving this is to have a start and finish pointer.

Start with all pointers pointing to the start of the string.

While finish + 1 does not equal '\0' increment the pointer "finish" - This is a way of getting the last charactor for later use

Next,
While "finish" > "start", swap chars using a temp variable, increment start, and decrement finish.

If you want I'll post some code I can, but it should be very easy for you to knock something up :)
 

click_here

Joined Sep 22, 2020
548
I should also say that
Code:
    char temp[5];
is not big enough to hold "HELLO", because of the nul charactor at the end of the string.

"HELLO" == {'H', 'E', 'L', 'L', 'O', '\0'}
 

drjohsmith

Joined Dec 13, 2021
1,601
It "can" be done without any temp variables

"just" swap pairs

"all" you have to do is work out which pairs to swap in what order.

Get a pack of cards,
lay out 5 different cards,
try to work out an algorithm
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
I present you with two challenges.

1) Write a function that returns the length of the string.
My soluation for your first challenge is as follow

C:
#include <stdio.h>

int String_length(char *buffer)
{
    int i = 0;
    
   for (i = 0; buffer[i] != '\0'; i++);
 
   return i;
 
}

int main(void)
{
    int length;
    char name [] = {"OLLEH"};   
    length = String_length(name);
    printf( "length  = %d", length);
    
    return 0;
}
length = 5
 

MrSalts

Joined Apr 2, 2020
2,767
It "can" be done without any temp variables

"just" swap pairs

"all" you have to do is work out which pairs to swap in what order.

Get a pack of cards,
lay out 5 different cards,
try to work out an algorithm
I don't get the point of this post. Handling ascii characters is much different that cares
 

drjohsmith

Joined Dec 13, 2021
1,601
I don't get the point of this post. Handling ascii characters is much different that cares
Can I check

" much different that cares "
you miss typed cards ?


If so,
what I am trying to suggest is to have the OP think about how to handle array elements,
in this case the elements are cards, as they are easier to handle,
could be different coloured counters, it does not matter

I'm also suggesting that they think about how an algorithm that swaps elements to reverse the order might be codded, may be as a set of instructions to a user,

IMHO, thinking of how to do the task is the key part of programming / engineering,
 

MrSalts

Joined Apr 2, 2020
2,767
Can I check

" much different that cares "
you miss typed cards ?


If so,
what I am trying to suggest is to have the OP think about how to handle array elements,
in this case the elements are cards, as they are easier to handle,
could be different coloured counters, it does not matter

I'm also suggesting that they think about how an algorithm that swaps elements to reverse the order might be codded, may be as a set of instructions to a user,

IMHO, thinking of how to do the task is the key part of programming / engineering,
See post 21. Your idea of using one card for each value cannot simulate that solution - the solution in post 21 solution requires no temp or storage registers. The second solution in post 21 doesn't even need a register to control the for loop.
 

click_here

Joined Sep 22, 2020
548
Not to muddy the water, but you can swap two integer values (like char) in place with the XOR function. To swap X and Y

X=X XOR Y
Y=Y XOR X
X=X XOR Y

https://en.m.wikipedia.org/wiki/XOR_swap_algorithm
Make sure that you don't try to change the same memory space, i.e.

<HELLO>
O<ELL>H
OL<L>EH <-- Here
OLLEH

With &x == &y, it becomes
x XOR x = 0
x XOR x = 0
x XOR x = 0

i.e.
{'O', 'L', 0x00, 'E', 'H', '\0' }

Also...
x = x + (x) = 2x
(x) = x - (x) = 0
x = x - (x) = -x

You may be able to tune your current algorithm to avoid it, but you would always need to do it for every algorithm you make with this swap in the future...

In my humble opinion, you are better off just using a temp variable.

Apart from this little feature mentioned above (which you can get around by putting the swap in an if statement), on modern machines the temp variable will usually get optimised away for use of the XCHG instruction, and your c code will be more readable in the end :)
 

djsfantasi

Joined Apr 11, 2010
9,237
Make sure that you don't try to change the same memory space, i.e.

<HELLO>
O<ELL>H
OL<L>EH <-- Here
OLLEH

With &x == &y, it becomes
x XOR x = 0
x XOR x = 0
x XOR x = 0

i.e.
{'O', 'L', 0x00, 'E', 'H', '\0' }

Also...
x = x + (x) = 2x
(x) = x - (x) = 0
x = x - (x) = -x

You may be able to tune your current algorithm to avoid it, but you would always need to do it for every algorithm you make with this swap in the future...

In my humble opinion, you are better off just using a temp variable.

Apart from this little feature mentioned above (which you can get around by putting the swap in an if statement), on modern machines the temp variable will usually get optimised away for use of the XCHG instruction, and your c code will be more readable in the end :)
The same restriction applies to MrSalts algorithm in post #21. It is a common problem with all (most) of the swap in place algorithms.

Swap in place can be done also with multiplication/ division and variable pointer implementations of this algorithm.

I mentioned it because of one interpretation of the TS problem. This approach can be useful in memory limited programs or as a possible speed improvements for large arrays.
 

djsfantasi

Joined Apr 11, 2010
9,237
There is no need to make your life more difficult than it already is.

Do this:

TEMP <- A
A <- B
B <- TEMP
Exactly!

The original statement has been over interpreted by me and others to mean no additional storage at all could be used. This confusion was also expressed in post #20.

The simple, obvious solution has been expressed by MrChips several times.
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
There is no need to make your life more difficult than it already is.

Do this:

TEMP <- A
A <- B
B <- TEMP
I can find length of string, program #29 and I know what's swaping, look at following code

C:
#include<stdio.h>
#include<stdlib.h>


int main (void)
{
  char temp;
  char A = '1';
  char B = '2';
 
  printf(" A = %c \n", A );
 
  printf(" B = %c \n", B );
 
  temp = A;
  A = B;
  B = temp;
 
  printf(" A = %c \n", A );
 
  printf(" B = %c \n", B );
  
  
   return 0;
}
A = 1
B = 2
A = 2
B = 1

I'm having trouble solving problem and can't figure out how to do it.
 
Top