swapping array elements in revereser order

MrChips

Joined Oct 2, 2009
30,821
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.
Solving what problem?

If you are referring to the string reversal problem, you have all the ingredients already. What else do you need besides outlining the algorithm itself.

You expect us to do everything for you?
 

Ian Rogers

Joined Dec 12, 2012
1,136
Just tried the XOR... Amazing... Why didn't I see that.... Also works with add / subtract...

sizeof(); will give the length of string + termination...
 

xox

Joined Sep 8, 2017
838
sizeof(); will give the length of string + termination...

That only works for arrays declared on the stack. For dynamic and/or aliased arrays, something like strlen() would be required. (Better yet: pass the length of the array explicitly.)

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.

Start with this:

Code:
void reverse(char* str, int len) {
  if (len == 0)
    len = strlen(str);
  for (int lft = 0, rgt = len; lft <= --rgt; ++lft) {
    printf("str[%d] = %c, str[%d] = %c \n", lft, str[lft], rgt, str[rgt]);
    /*
     ...reverse elements here...
    */
  }
}

Does that help?
 

Ian Rogers

Joined Dec 12, 2012
1,136
That only works for arrays declared on the stack. For dynamic and/or aliased arrays, something like strlen() would be required. (Better yet: pass the length of the array explicitly.)
Has always worked for me... In fact the little program I used to "test" said XOR function used it sucessfully with a statically declared array..

In the documentation it states that the sizeof operator can be used on arrays.. BUT!! I do not disbelieve you, only if you have an example I would like to try it out... One of those gotcha type errors...

NOTE**
I have re-read the rules of sizeof() It won't work on dynamically allocated arrays as it calculates at compile time... Which is why it always works for me..
 

click_here

Joined Sep 22, 2020
548
Has always worked for me... In fact the little program I used to "test" said XOR function used it sucessfully with a statically declared array..

In the documentation it states that the sizeof operator can be used on arrays.. BUT!! I do not disbelieve you, only if you have an example I would like to try it out... One of those gotcha type errors...

NOTE**
I have re-read the rules of sizeof() It won't work on dynamically allocated arrays as it calculates at compile time... Which is why it always works for me..
Try it with this...
Code:
char apple[100] = "Banana";
 

Ian Rogers

Joined Dec 12, 2012
1,136
That's different.... No need for a sizeof() if you specify the actual array size..

char apple[] = {"banana"};
len = sizeof(apple); would work for the example given..
 

djsfantasi

Joined Apr 11, 2010
9,163
Try it with this...
Code:
char apple[100] = "Banana";
This pseudo-code resolves the banana problem and the HELLO problem

Code:
int i, j, maxswaps, maxchars;
char temp;
for ( i=0; i<100; i++) {
 if (apple[i] == 0) break;
}
maxchars=i;
maxswaps=int(float(maxchars))/2.0)
for (int i=0; i < maxswaps; i++) {
j=maxchars-(i +1);
temp=apple[j];
apple[j]=apple[i];
apple[i]=temp;
}
…or something like that.
 

MrChips

Joined Oct 2, 2009
30,821
There is no need to use float division as in
maxswaps=int(float(maxchars))/2.0)

Integer divide gives the desired result that works for both odd or even string length:
maxswaps = lenght(string)/2;
 

click_here

Joined Sep 22, 2020
548
That's different.... No need for a sizeof() if you specify the actual array size..

char apple[] = {"banana"};
len = sizeof(apple); would work for the example given..
Not if the string changes...
Code:
char apple[] = "banana"; 
...
strncpy(apple, "pea", sizeof(apple));  
...
len = sizeof(apple);
...And if you can guarantee that the string won't change, you'd already know the size of the string :)

In any account sizeof is the wrong tool for the job.

The best option is to just use strlen() from the string.h library, or like in the case of the TS where they can't use a library function, just make your own. i.e. Count how many charactors there are before the '\0' in a loop.
 

click_here

Joined Sep 22, 2020
548
I should also add that it is very unusual that the sizeof a buffer where a string is held is the size of the string.

However, there is a case where you are holding a particular constant string, but in that case you probably wouldn't want to reverse it (or modify it in any way)
 

xox

Joined Sep 8, 2017
838
That's different.... No need for a sizeof() if you specify the actual array size..

char apple[] = {"banana"};
len = sizeof(apple); would work for the example given..
Code:
void check(char buf[]) {
  printf("sizeof(buf) = %lu\n", sizeof(buf));
}

int main(void) {
  char buf[1024] = "Hello World!";
  printf("sizeof(buf) = %lu\n", sizeof(buf));
  printf("strlen(buf) = %lu\n", strlen(buf));
  check(buf); 
}
Output:

test.c: In function ‘check’:
test.c:49:39: warning: ‘sizeof’ on array function parameter ‘buf’ will return size of ‘char *’ [-Wsizeof-array-argument]
printf("sizeof(buf) = %lu\n", sizeof(buf));
^
test.c:48:17: note: declared here
void check(char buf[]) {
^~~
sizeof(buf) = 1024
strlen(buf) = 12
sizeof(buf) = 8
 

Ian Rogers

Joined Dec 12, 2012
1,136
This is why I don't frequent this place as much as I'd like...

My example works for me, but members here try so hard to be "the better" person... All this crap does not help the op. If I use the sizeof(); as shown, it works.. Why spend a lifetime trying to putting others down... If you read the first post, he didn't even want to use library functions anyway..... And as far as I'm concerned, sizeof() is as unuseful as rocking horse poo as it only works at compile time , where (as already stated ) you already know the size.... It will only be used for structs and classes where I can't be arsed working it out.

Done here now..
 

click_here

Joined Sep 22, 2020
548
This is why I don't frequent this place as much as I'd like...

My example works for me, but members here try so hard to be "the better" person... All this crap does not help the op. If I use the sizeof(); as shown, it works.. Why spend a lifetime trying to putting others down... If you read the first post, he didn't even want to use library functions anyway..... And as far as I'm concerned, sizeof() is as unuseful as rocking horse poo as it only works at compile time , where (as already stated ) you already know the size.... It will only be used for structs and classes where I can't be arsed working it out.

Done here now..
I'm sorry you feel that way, but there were problems with your suggestion of using 'sizeof' to determine a string's length.

Pointing out such problems is important to help the TS, as they might become confused as to why the suggested solution is not working when they (say) get a random string from the user.

I hope to see you on here again soon :)
 

MrChips

Joined Oct 2, 2009
30,821
TS had indicated that library functions are not to be used.
Fortunately, writing your own strlen( ) function is not difficult and TS has done it.
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
soluation
C:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char *reverse(char *buffer, int length)
{
    char *low = buffer;               
    char *high = buffer + length - 1; 
    char  temp;

    while (low < high)
    {
        temp = *low;
        *low = *high;
        *high = temp;

        low++;
        high--;
    }

    return buffer;
}

int main(void)
{
    int i = 0;
    
    char  name[] = "54321";
    
    for (i = 0;  name[i] != '\0'; i++);

    reverse(name, i);

    printf("%s\n", name);

    return 0;
}
 

MrSalts

Joined Apr 2, 2020
2,767
soluation
C:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char *reverse(char *buffer, int length)
{
    char *low = buffer;               
    char *high = buffer + length - 1; 
    char  temp;

    while (low < high)
    {
        temp = *low;
        *low = *high;
        *high = temp;

        low++;
        high--;
    }

    return buffer;
}

int main(void)
{
    int i = 0;
    
    char  name[] = "54321";
    
    for (i = 0;  name[i] != '\0'; i++);

    reverse(name, i);

    printf("%s\n", name);

    return 0;
}
Good work. Looks like you've met some of the original criteria.
 
Top