# swapping array elements in revereser order

Joined Feb 4, 2022
127
Hello

I want to swap array elements as shown in following image without any library, sizeof operator and extra array

C:
#include <stdio.h>

int main()
{
int i = 0;
char name[]= {"OLLEH"};

char *start = &name[0];
char *end   = &name[4];

for ( i = 0; i < 5; i++)
{
start = *end ;

printf("%c\n", start);
end--;

}

printf("%s", name);
return 0;
}
H
E
L
L
O
OLLEH

I was expecting print statement should be print "HELLO" but it's only printing the same as before OLLEH. I don't understand why it happens in the code. any idea how to get output as expected

#### MrChips

Joined Oct 2, 2009
26,134
Variable name this the original array of the letters "OLLEH".
It was never altered by the code.

Your program is flawed in two ways.
1) It does not alter any array.
2) If it did, it would destroy half of the array.

When attempting to create an algorithm, sometimes you can check your results by working it through manually (i.e. without a computer program).

Joined Feb 4, 2022
127
Variable name this the original array of the letters "OLLEH".
It was never altered by the code.

Your program is flawed in two ways.
1) It does not alter any array.
2) If it did, it would destroy half of the array.
Why I don't get last two characters in string ?

C:
#include <stdio.h>

int main()
{
int i = 0;

char name[]= {"OLLEH"};

char *end   = &name[4];

for ( i = 0; i < 4; i++)
{
name[i] = *end;
end--;

}

printf("%s", name);

return 0;
}
HELEH

#### MrChips

Joined Oct 2, 2009
26,134
Like I said before, because you have destroyed the original array.
Write to a new array instead of the original array.

Edit:
If you want to do the reversal in-place, then you have to swap the pair at the same time using temporary storage. In this case you only need to loop for half the number of characters. (Think about what is going to happen when there is an odd number of characters.)

Joined Feb 4, 2022
127
Write to a new array instead of the original array.
Like I said before, I want to get reverse string without any library, sizeof operator, new function and new array.

Can it be achieved If yes how can it be achieved.

#### djsfantasi

Joined Apr 11, 2010
8,330
Like I said before, I want to get reverse string without any library, sizeof operator, new function and new array.

Can it be achieved If yes how can it be achieved.
Yes…

Before this question can be answered, you have to show the original homework statement. Take a picture of it and upload it here.

#### AlbertHall

Joined Jun 4, 2014
12,023
Like I said before, because you have destroyed the original array.
Write to a new array instead of the original array.
You could do this without a separate array.
Temporarily store the first element.
Transfer last element to first place
Store temporary into last place.
Repeat for second and second last elements until your start pointer >= end pointer.

Joined Feb 4, 2022
127
Yes…

Before this question can be answered, you have to show the original homework statement. Take a picture of it and upload it here.
i don't have anything like that I thought I should solve the simple problem first.

You could do this without a separate array.
Temporarily store the first element.
Transfer last element to first place
Store temporary into last place.
Repeat for second and second last elements until your start pointer >= end pointer.
Pointer can point to the memory location of the variable we can get the value pointed by pointer by using pointer dereference.

I have declared two pointer variables start and end. The start pointer points to the first position of the array and the end pointer points to the last position of the array.

i'm stuck i don't know what to do

#### MrChips

Joined Oct 2, 2009
26,134
Do this with pencil and paper first.

[O] [L] [L] [E] [H]

Take H
Erase O and replace it with H.

Take E ... repeat as above.
What is the result?

#### MrChips

Joined Oct 2, 2009
26,134

for ( i = 0; i < 5; i++)

how did you determine that there were 5 characters?

#### MrSalts

Joined Apr 2, 2020
1,762
I couldn't see a way without adding a variable to hold the last element.

Code:
#include <stdio.h>

int main()
{
char name[]= {"OLLEH"};
char temp;
char *end   = &name[4];
for (int j=0;j<5;j++ ){
temp = name[4];
for (int i = 3; i > j-1; i--)
{
name[i+1] = name[i];
}
name[j] = temp;
}
printf("%s", name);

return 0;
}

#### AlbertHall

Joined Jun 4, 2014
12,023
I couldn't see a way without adding a variable to hold the last element.
An extra array isn't allowed but I don't think that an extra element storage is disallowed.

#### djsfantasi

Joined Apr 11, 2010
8,330
An extra array isn't allowed but I don't think that an extra element storage is disallowed.
Did anyone say the array definition couldn’t have an “extra” element that could be used as temporary storage? The indices are hard-coded. So what if you add an “X” to the array definition and use name[5] for temporary storage?

#### djsfantasi

Joined Apr 11, 2010
8,330
Did anyone say the array definition couldn’t have an “extra” element that could be used as temporary storage? The indices are hard-coded. So what if you add an “X” to the array definition and use name[5] for temporary storage?
You could use a define to set tempValue to name[5]…

Just sayin’

#### MrSalts

Joined Apr 2, 2020
1,762
With extra temp variable as name[5] and then filled with null until the last line.

Code:
#include <stdio.h>

int main()
{
char name[]= {"OLLEH "};
char temp;
char *end   = &name[4];
for (int j=0;j<5;j++ ){
name[5] = name[4];
for (int i = 3; i > j-1; i--)
{
name[i+1] = name[i];
}
name[j] = name[5];
}
name[5] = 0x00;
printf("%s", name);

return 0;
}

Last edited:

#### djsfantasi

Joined Apr 11, 2010
8,330
I’m still convinced this is homework and I wouldn’t have supplied the code for the answer.

#### MrSalts

Joined Apr 2, 2020
1,762
I find it entertaining to get people past the "drop date", after that, the transcript gets a W for withdrawal or the student thinks they are in good enough shape to move forward and end up getting an F on the final.

Last edited:

Joined Feb 4, 2022
127

for ( i = 0; i < 5; i++)

how did you determine that there were 5 characters?
name[0] = 'O'
name[1] = 'L'
name[2] = 'L'
name[3] = 'E'
name[4] = 'H'
name[5] = '/0'

An extra array isn't allowed but I don't think that an extra element storage is disallowed.
You could do this without a separate array.
I already knew the method MrSalt told, that's why I said I don't wanted to use separate array.

@AlbertHall, In your first statement you said it can done without separate array. can you tell me process how to do this ? if it's not possible then please clearify

#### MrChips

Joined Oct 2, 2009
26,134
name[0] = 'O'
name[1] = 'L'
name[2] = 'L'
name[3] = 'E'
name[4] = 'H'
name[5] = '/0'

I already knew the method MrSalt told, that's why I said I don't wanted to use separate array.

@AlbertHall, In your first statement you said it can done without separate array. can you tell me process how to do this ? if it's not possible then please clearify
You saw visually that there were 5 characters. That is not how computers function. Your computer does not have eyes to see how many characters are in the array. You need to program in that function.

You do not need a separate array.
Do you know how to interchange the contents of two boxes?
You need to figure out problems like this on your own.

I will give you the solution:

Put A into TEMP
Put B into A
Put TEMP into B

#### MrSalts

Joined Apr 2, 2020
1,762
I already knew the method MrSalt told, that's why I said I don't wanted to use separate array.
I'm sure you did. Now, the only array in the solution above is name[ ]. If you want to change the definition of "array", that would be pretty funny. An "array element" is way different than "array".

If dditional "array elements" are not allowed AND additional "arrays" are not allowed, please make sure you list ALL of the constraints that you plan on adding in the future so we can solve the problem you really have instead of the problem with a partial list of constraints that you have described so far.