Cancel offer in C?

I just wrote a program that cancels the sentence that the user gives. For example: if the user enters "Like you", my program generates "uoy era woH".

The program I wrote is shown below. I just have a wild intuition that there may be a smarter program than this. So your valuable contribution will be appreciated, or any better program than this is also very welcome.

int ReverseString(char *);
main() {
  char *Str;
  printf("enter any string\n");
  gets(Str);
  ReverseString(Str);
  getch();
}
int ReverseString(char *rev) {
  int len = 0;
  char p;
  while(*rev!='\0') {
    len++;
    rev++;
  }
  rev--;
  while(len>0) {
    p = *rev;
    putchar(p);
    rev--;
    len--;
  }
}

Many thanks.

+2
source share
9 answers

You can use recursion.

int ReverseString(char *rev) {
  if(*rev!='\0') {
    ReverseString(rev + 1);
    putchar(*rev);
  }
  return 1;
}
+7
source
void ReverseString( char* str, int len ) {
  if( len > 1 ) {
    swap( &str[0], &str[len - 1] );
    ReverseString( ++str, len - 2 );
  }
}

Or by deploying tail recursion:

void ReverseString( char* str, int len ) {
  while( len > 1 ) {
    swap( &str[0], &str[len - 1] );
    ++str;
    len -= 2;
  }
}

If swap is defined as:

void swap( char* a, char* b ) {
  *a ^= *b;
  *b ^= *a;
  *a ^= *b;
}

, TA , :)

+3

. .

char *Str;
printf("enter any string\n");
gets(Str);

:

char str[81]={0};
printf("Enter any string up to 80 characters\n");
scanf("%80s\n",str);
ReverseString(str)

, .

0

, . , .

char* reverse(char *string){

    int length = 0;
    int half = 0;

    length = strlen(string);
    half = (length/2) - 1;
    --length;

    int i = 0;
    register char interim;
    for(; i<=half; ++i){
        interim = string[i];
        string[i] = string[length - i];
        string[length - i] = interim;
    }

    return string;

}

, , , . , , , .

, , , length , i length . , .

, bash register: P

0

:

void reverse_string(char* str) {
  char* p2 = str;
  while (*p2 != '\0') {
    /* assumes the string is null-terminated, will fail otherwise */
    ++p2;
  }
  --p2;
  char* p1 = str;
  while (p1 < p2) {
    char tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
    ++p1;
    --p2;
  }
}

:

void reverse_string(char* str, const size_t len) {
  if (len <= 1) {
    return;
  }
  char* p2 = str + len - 1;
  char* p1 = str;
  while (p1 < p2) {
    char tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
    ++p1;
    --p2;
  } 
}
0

The following program prints its arguments in reverse character order:

#include <string.h>
#include <stdio.h>

char * reverse(char * string) {
  char * a = string;
  char * b = string + strlen(string) - 1;
  for(; a < b; ++a, --b)
    *a ^= *b, *b ^= *a, *a ^= *b; // swap *a <-> *b
  return string;
}

int main(int argc, char * argv[]) {
  for(int i = 1; i < argc; ++i)
    puts(reverse(argv[i]));
}

Nothing new here, but IMO is more readable than most of the other answers.

0
source
void revstr(TCHAR *str) {
  if( *str == '\0' ) {
    return;
  }
  TCHAR *start = str;
  TCHAR *end = start + strlen(str) - 1;
  while(start < end) {
    *start ^= *end;
    *end ^= *start;
    *start ^= *end;
    *start++;
    *end-–;
    /*
      could also use *start ^= *end ^= *start++ ^= *end–-; if you want to get fancy
    */
  }
}

Stolen from the 2005 version for himself , but screw this guy, he was sleeping with my wife. Yes, I know that I do not need some of the "*", but I first wrote a single line font and just converted it, and the one liner really requires them.

0
source

Another change ...

void ReverseString( char *str, int len ) {
  int i;
  for(i=0; i < len/2; i++) {
    swap( &str[i], &str[len -1 -i] );
  }
}
void swap( char *a, char *b ) {
  char tmp = *a;
  *a = *b;
  *b = tmp;
}
0
source
#include<stdio.h>
void reverse(char s[])
{
        int i=0,j,x=0,z;
        printf("\nThe string is : ");
        printf("%s",s);
        printf("\nThe reverse string is : ");
        while(s[i] != ' ')
        {
                while(s[i] != ' ')
                        i++;
                z=i+1;
                for(j=i-1;j>=x;j--)
                        printf("%c",s[j]);
                printf(" ");
                i=z;
                x=z;
        }
}
main()
{
char s[50];
int a;
for(a=0;a<50;a++)
        s[a]=' ';
puts("\nEnter a sentence : ");
fgets(s,50,stdin);
reverse(s);
}
-1
source

All Articles